- All Implemented Interfaces:
DatabaseAccess
- Direct Known Subclasses:
CloseableDatabase
DatabaseAccess
each individual call is a separate transaction.
If the current thread is already in a transaction, the calls will be performed using the connection associated with that transaction.
For transactions across multiple statements, use DatabaseConnection
.- Author:
- AO Industries, Inc.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.aoapps.dbc.DatabaseAccess
DatabaseAccess.Null
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
close()
Closes the database.connect()
Creates a newDatabaseConnection
instance.final DatabaseConnection
Deprecated, for removal: This API element is subject to removal in a future version.protected void
deinitConnection
(Connection conn) Before a connection is released back to the pool or the dataSource, it is passed here for any custom de-initialization routine.protected void
deinitSqlDataTypes
(Connection conn) Before a connection is release back to the pool or the dataSource, it is passed here for de-initialization ofgetSqlDataTypes()
.doubleStream
(int isolationLevel, boolean readOnly, String sql, Object... params) Query the database with aDoubleStream
return type.final <V> V
executeTransaction
(DatabaseCallable<V> callable) Deprecated, for removal: This API element is subject to removal in a future version.final void
executeTransaction
(DatabaseRunnable runnable) Deprecated, for removal: This API element is subject to removal in a future version.final <V,
Ex extends Exception>
VexecuteTransaction
(Class<Ex> exClass, DatabaseCallableE<V, Ex> callable) Deprecated, for removal: This API element is subject to removal in a future version.final <Ex extends Exception>
voidexecuteTransaction
(Class<Ex> exClass, DatabaseRunnableE<Ex> runnable) Deprecated, for removal: This API element is subject to removal in a future version.Gets a read/write connection to the database with a transaction level ofConnections.DEFAULT_TRANSACTION_ISOLATION
, warning when a connection is already used by this thread.getConnection
(boolean readOnly) Gets a connection to the database with a transaction level ofConnections.DEFAULT_TRANSACTION_ISOLATION
, warning when a connection is already used by this thread.getConnection
(int maxConnections) Gets a read/write connection to the database with a transaction level ofConnections.DEFAULT_TRANSACTION_ISOLATION
.getConnection
(int isolationLevel, boolean readOnly) Gets a connection to the database, warning when a connection is already used by this thread.getConnection
(int isolationLevel, boolean readOnly, int maxConnections) Gets a connection to the database.protected FailFastConnection
getConnection
(int isolationLevel, boolean readOnly, int maxConnections, boolean allowClose) Gets the pool ornull
if using aDataSource
.Gets the data source ornull
if using anAOConnectionPool
.protected void
initConnection
(Connection conn) Whenever a new connection is obtained from the pool or the dataSource, it is passed here for any custom initialization routine.protected void
initSqlDataTypes
(Connection conn) Whenever a new connection is obtained from the pool or the dataSource, it is passed here for initialization ofgetSqlDataTypes()
.Query the database with anIntStream
return type.boolean
Checks if the current thread is in a transaction.long
largeUpdate
(String sql, Object... params) Performs an update on the database and returns the number of rows affected.longStream
(int isolationLevel, boolean readOnly, String sql, Object... params) Query the database with aLongStream
return type.<T,
Ex extends Throwable>
TqueryCall
(int isolationLevel, boolean readOnly, Class<? extends Ex> exClass, ResultSetCallableE<? extends T, ? extends Ex> resultSetCallable, String sql, Object... params) Query the database, calling theResultSetCallableE
once.protected void
release
(FailFastConnection conn) Closes and/or releases the given connection back to the pool.final void
releaseConnection
(Connection conn) Deprecated, for removal: This API element is subject to removal in a future version.Please release to the pool by closing the connection, preferably via try-with-resources.stream
(int isolationLevel, boolean readOnly, Class<? extends Ex> exClass, ObjectFactoryE<? extends T, ? extends Ex> objectFactory, String sql, Object... params) Query the database with aStream<T>
return type, objects are created with the provided factory.toString()
<V,
Ex extends Throwable>
VtransactionCall
(Class<? extends Ex> exClass, DatabaseCallableE<? extends V, ? extends Ex> callable) Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.int
Performs an update on the database and returns the number of rows affected.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.aoapps.dbc.DatabaseAccess
doubleStream, doubleStreamUpdate, executeBigDecimalQuery, executeBigDecimalQuery, executeBigDecimalUpdate, executeBooleanQuery, executeBooleanQuery, executeBooleanUpdate, executeByteArrayQuery, executeByteArrayQuery, executeByteArrayUpdate, executeDateQuery, executeDateQuery, executeDateUpdate, executeIntListQuery, executeIntListQuery, executeIntListUpdate, executeIntQuery, executeIntQuery, executeIntUpdate, executeLongListQuery, executeLongListQuery, executeLongListUpdate, executeLongQuery, executeLongQuery, executeLongUpdate, executeObjectCollectionQuery, executeObjectCollectionQuery, executeObjectCollectionQuery, executeObjectCollectionQuery, executeObjectCollectionQuery, executeObjectCollectionQuery, executeObjectCollectionUpdate, executeObjectCollectionUpdate, executeObjectCollectionUpdate, executeObjectListQuery, executeObjectListQuery, executeObjectListQuery, executeObjectListQuery, executeObjectListQuery, executeObjectListQuery, executeObjectListUpdate, executeObjectListUpdate, executeObjectListUpdate, executeObjectQuery, executeObjectQuery, executeObjectQuery, executeObjectQuery, executeObjectQuery, executeObjectQuery, executeObjectUpdate, executeObjectUpdate, executeObjectUpdate, executeQuery, executeQuery, executeQuery, executeQuery, executeShortListQuery, executeShortListQuery, executeShortListUpdate, executeShortQuery, executeShortQuery, executeShortUpdate, executeStringListQuery, executeStringListQuery, executeStringListUpdate, executeStringQuery, executeStringQuery, executeStringUpdate, executeTimestampQuery, executeTimestampQuery, executeTimestampUpdate, executeUpdate, executeUpdate, executeUpdate, intStream, intStreamUpdate, longStream, longStreamUpdate, queryBigDecimal, queryBigDecimal, queryBigDecimal, queryBigDecimalNullable, queryBigDecimalOptional, queryBigDecimalOptionalNullable, queryBoolean, queryBoolean, queryBoolean, queryBooleanNullable, queryBooleanOptional, queryBooleanOptionalNullable, queryByteArray, queryByteArray, queryByteArray, queryByteArrayNullable, queryByteArrayOptional, queryByteArrayOptionalNullable, queryCall, queryCall, queryCall, queryCollection, queryCollection, queryCollection, queryCollection, queryDate, queryDate, queryDate, queryDateNullable, queryDateOptional, queryDateOptionalNullable, queryDouble, queryDouble, queryDouble, queryDoubleNullable, queryDoubleOptional, queryDoubleOptionalNullable, queryFloat, queryFloat, queryFloat, queryFloatNullable, queryFloatOptional, queryFloatOptionalNullable, queryInt, queryInt, queryInt, queryIntList, queryIntList, queryIntNullable, queryIntOptional, queryIntOptionalNullable, queryList, queryList, queryList, queryList, queryLong, queryLong, queryLong, queryLongList, queryLongList, queryLongNullable, queryLongOptional, queryLongOptionalNullable, queryNewCollection, queryNewCollection, queryNewCollection, queryNewCollection, queryObject, queryObject, queryObject, queryObject, queryObject, queryObject, queryObjectNullable, queryObjectNullable, queryObjectOptional, queryObjectOptional, queryObjectOptionalNullable, queryObjectOptionalNullable, queryRun, queryRun, queryRun, queryRun, queryShort, queryShort, queryShort, queryShortNullable, queryShortOptional, queryShortOptionalNullable, queryString, queryString, queryString, queryStringList, queryStringList, queryStringNullable, queryStringOptional, queryStringOptionalNullable, queryTimestamp, queryTimestamp, queryTimestamp, queryTimestampNullable, queryTimestampOptional, queryTimestampOptionalNullable, stream, stream, stream, streamOptional, streamOptional, streamOptional, streamOptional, streamOptionalUpdate, streamOptionalUpdate, streamString, streamString, streamStringUpdate, streamUpdate, streamUpdate, transactionCall, transactionCall, transactionCall, transactionRun, transactionRun, transactionRun, transactionRun, updateBigDecimal, updateBigDecimalNullable, updateBigDecimalOptional, updateBigDecimalOptionalNullable, updateBoolean, updateBooleanNullable, updateBooleanOptional, updateBooleanOptionalNullable, updateByteArray, updateByteArrayNullable, updateByteArrayOptional, updateByteArrayOptionalNullable, updateCall, updateCall, updateCollection, updateCollection, updateDate, updateDateNullable, updateDateOptional, updateDateOptionalNullable, updateDouble, updateDoubleNullable, updateDoubleOptional, updateDoubleOptionalNullable, updateFloat, updateFloatNullable, updateFloatOptional, updateFloatOptionalNullable, updateInt, updateIntList, updateIntNullable, updateIntOptional, updateIntOptionalNullable, updateList, updateList, updateLong, updateLongList, updateLongNullable, updateLongOptional, updateLongOptionalNullable, updateNewCollection, updateNewCollection, updateObject, updateObject, updateObjectNullable, updateObjectNullable, updateObjectOptional, updateObjectOptional, updateObjectOptionalNullable, updateObjectOptionalNullable, updateRun, updateRun, updateShort, updateShortNullable, updateShortOptional, updateShortOptionalNullable, updateString, updateStringList, updateStringNullable, updateStringOptional, updateStringOptionalNullable, updateTimestamp, updateTimestampNullable, updateTimestampOptional, updateTimestampOptionalNullable
-
Constructor Details
-
Database
-
Database
-
Database
-
-
Method Details
-
createDatabaseConnection
Deprecated, for removal: This API element is subject to removal in a future version.Please use eitherconnect()
or one of the various call/run methods, which enforce the transaction semantics. -
connect
Creates a newDatabaseConnection
instance. The instance must be closed viaDatabaseConnection.close()
orDatabaseConnection.close(java.lang.Throwable)
, typically in a try/catch or try-with-resources block.Note that the close methods will rollback any transaction in progress, so it is up to the caller to perform any necessary
DatabaseConnection.commit()
.- See Also:
-
getConnectionPool
Gets the pool ornull
if using aDataSource
.- See Also:
-
getDataSource
Gets the data source ornull
if using anAOConnectionPool
.- See Also:
-
close
protected void close()Closes the database.- See Also:
-
initSqlDataTypes
Whenever a new connection is obtained from the pool or the dataSource, it is passed here for initialization ofgetSqlDataTypes()
.- Throws:
SQLException
- See Also:
-
deinitSqlDataTypes
Before a connection is release back to the pool or the dataSource, it is passed here for de-initialization ofgetSqlDataTypes()
.- Throws:
SQLException
- See Also:
-
initConnection
Whenever a new connection is obtained from the pool or the dataSource, it is passed here for any custom initialization routine.This default implementation does nothing.
- Throws:
SQLException
- See Also:
-
deinitConnection
Before a connection is released back to the pool or the dataSource, it is passed here for any custom de-initialization routine.This default implementation does nothing.
- Throws:
SQLException
- See Also:
-
getConnection
Gets a read/write connection to the database with a transaction level ofConnections.DEFAULT_TRANSACTION_ISOLATION
, warning when a connection is already used by this thread.The connection will be in auto-commit mode, as configured by
AOConnectionPool.resetConnection(java.sql.Connection)
(or compatibleDataSource
implementation viaAOConnectionPool.defaultResetConnection(java.sql.Connection)
).When obtaining a connection from a
DataSource
, if the connection is not in auto-commit mode, a warning will be logged, then the connection will be rolled-back and set to auto-commit.If all the connections in the pool are busy and the pool is at capacity, waits until a connection becomes available.
The connection will be a
FailFastConnection
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The fail-fast connection is used to determine whether to roll-back or commit during automatic transaction management.The connection will also be a
ConnectionTracker
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The connection tracking is used to close/free all objects before returning the connection to the underlying pool.- Returns:
- The read/write connection to the database
- Throws:
SQLException
- when an error occurs, or when a thread attempts to allocate more than half the pool- See Also:
-
getConnection
Gets a read/write connection to the database with a transaction level ofConnections.DEFAULT_TRANSACTION_ISOLATION
.The connection will be in auto-commit mode, as configured by
AOConnectionPool.resetConnection(java.sql.Connection)
(or compatibleDataSource
implementation viaAOConnectionPool.defaultResetConnection(java.sql.Connection)
).When obtaining a connection from a
DataSource
, if the connection is not in auto-commit mode, a warning will be logged, then the connection will be rolled-back and set to auto-commit.If all the connections in the pool are busy and the pool is at capacity, waits until a connection becomes available.
The connection will be a
FailFastConnection
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The fail-fast connection is used to determine whether to roll-back or commit during automatic transaction management.The connection will also be a
ConnectionTracker
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The connection tracking is used to close/free all objects before returning the connection to the underlying pool.- Parameters:
maxConnections
- The maximum number of connections expected to be used by the current thread. This should normally be one to avoid potential deadlock.The connection will continue to be considered used by the allocating thread until released (via
Connection.close()
, even if the connection is shared by another thread.- Returns:
- The read/write connection to the database
- Throws:
SQLException
- when an error occurs, or when a thread attempts to allocate more than half the pool- See Also:
-
getConnection
Gets a connection to the database with a transaction level ofConnections.DEFAULT_TRANSACTION_ISOLATION
, warning when a connection is already used by this thread.The connection will be in auto-commit mode, as configured by
AOConnectionPool.resetConnection(java.sql.Connection)
(or compatibleDataSource
implementation viaAOConnectionPool.defaultResetConnection(java.sql.Connection)
).When obtaining a connection from a
DataSource
, if the connection is not in auto-commit mode, a warning will be logged, then the connection will be rolled-back and set to auto-commit.If all the connections in the pool are busy and the pool is at capacity, waits until a connection becomes available.
The connection will be a
FailFastConnection
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The fail-fast connection is used to determine whether to roll-back or commit during automatic transaction management.The connection will also be a
ConnectionTracker
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The connection tracking is used to close/free all objects before returning the connection to the underlying pool.- Parameters:
readOnly
- Theread-only flag
- Returns:
- The connection to the database
- Throws:
SQLException
- when an error occurs, or when a thread attempts to allocate more than half the pool- See Also:
-
getConnection
Gets a connection to the database, warning when a connection is already used by this thread.The connection will be in auto-commit mode, as configured by
AOConnectionPool.resetConnection(java.sql.Connection)
(or compatibleDataSource
implementation viaAOConnectionPool.defaultResetConnection(java.sql.Connection)
).When obtaining a connection from a
DataSource
, if the connection is not in auto-commit mode, a warning will be logged, then the connection will be rolled-back and set to auto-commit.If all the connections in the pool are busy and the pool is at capacity, waits until a connection becomes available.
The connection will be a
FailFastConnection
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The fail-fast connection is used to determine whether to roll-back or commit during automatic transaction management.The connection will also be a
ConnectionTracker
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The connection tracking is used to close/free all objects before returning the connection to the underlying pool.- Parameters:
isolationLevel
- Thetransaction isolation level
readOnly
- Theread-only flag
- Returns:
- The connection to the database
- Throws:
SQLException
- when an error occurs, or when a thread attempts to allocate more than half the pool- See Also:
-
getConnection
public Connection getConnection(int isolationLevel, boolean readOnly, int maxConnections) throws SQLException Gets a connection to the database.The connection will be in auto-commit mode, as configured by
AOConnectionPool.resetConnection(java.sql.Connection)
(or compatibleDataSource
implementation viaAOConnectionPool.defaultResetConnection(java.sql.Connection)
).When obtaining a connection from a
DataSource
, if the connection is not in auto-commit mode, a warning will be logged, then the connection will be rolled-back and set to auto-commit.If all the connections in the pool are busy and the pool is at capacity, waits until a connection becomes available.
The connection will be a
FailFastConnection
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The fail-fast connection is used to determine whether to roll-back or commit during automatic transaction management.The connection will also be a
ConnectionTracker
, which may be unwrapped viaWrapper.unwrap(java.lang.Class)
. The connection tracking is used to close/free all objects before returning the connection to the underlying pool.- Parameters:
isolationLevel
- Thetransaction isolation level
readOnly
- Theread-only flag
maxConnections
- The maximum number of connections expected to be used by the current thread. This should normally be one to avoid potential deadlock.The connection will continue to be considered used by the allocating thread until released (via
Connection.close()
, even if the connection is shared by another thread.- Returns:
- The connection to the database
- Throws:
SQLException
- when an error occurs, or when a thread attempts to allocate more than half the pool- See Also:
-
getConnection
protected FailFastConnection getConnection(int isolationLevel, boolean readOnly, int maxConnections, boolean allowClose) throws SQLException - Throws:
SQLException
-
releaseConnection
@Deprecated(forRemoval=true) public final void releaseConnection(Connection conn) throws SQLException Deprecated, for removal: This API element is subject to removal in a future version.Please release to the pool by closing the connection, preferably via try-with-resources.- Throws:
SQLException
-
release
Closes and/or releases the given connection back to the pool. Any transaction in-progress is rolled-back.When there is no fail-fast cause, the connection is immediately deinit'ed then closed (which returns the connection to the underlying pool).
Otherwise, when there remains an unresolved fail-fast cause, the connection is validated in the background before being either aborted or deinit'ed then closed (both of which return the connection to the underlying pool).
- Throws:
SQLException
- See Also:
-
getLogger
-
isInTransaction
public boolean isInTransaction()Description copied from interface:DatabaseAccess
Checks if the current thread is in a transaction.- Specified by:
isInTransaction
in interfaceDatabaseAccess
- See Also:
-
executeTransaction
@Deprecated(forRemoval=true) public final <V> V executeTransaction(DatabaseCallable<V> callable) throws SQLException Deprecated, for removal: This API element is subject to removal in a future version.- Throws:
SQLException
-
transactionCall
public <V,Ex extends Throwable> V transactionCall(Class<? extends Ex> exClass, DatabaseCallableE<? extends V, ? extends Ex> callable) throws SQLException, ExDescription copied from interface:DatabaseAccess
Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.- Rolls-back the transaction on
NoRowException
,NullDataException
, orExtraRowException
on the outer-most transaction only. - Rolls-back the transaction on all other
Throwable
.
The connection allocated is stored as a
ThreadLocal
and will be automatically reused if another transaction is performed within this transaction. Any nested transaction will automatically become part of the enclosing transaction. For safety, a nested transaction will still rollback the entire transaction on any exception.- Specified by:
transactionCall
in interfaceDatabaseAccess
- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex
- See Also:
- Rolls-back the transaction on
-
executeTransaction
@Deprecated(forRemoval=true) public final <V,Ex extends Exception> V executeTransaction(Class<Ex> exClass, DatabaseCallableE<V, Ex> callable) throws SQLException, ExDeprecated, for removal: This API element is subject to removal in a future version.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex
-
executeTransaction
@Deprecated(forRemoval=true) public final void executeTransaction(DatabaseRunnable runnable) throws SQLException Deprecated, for removal: This API element is subject to removal in a future version.- Throws:
SQLException
-
executeTransaction
@Deprecated(forRemoval=true) public final <Ex extends Exception> void executeTransaction(Class<Ex> exClass, DatabaseRunnableE<Ex> runnable) throws SQLException, Ex Deprecated, for removal: This API element is subject to removal in a future version.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex
-
toString
-
doubleStream
public DoubleStream doubleStream(int isolationLevel, boolean readOnly, String sql, Object... params) throws NullDataException, SQLException Description copied from interface:DatabaseAccess
Query the database with aDoubleStream
return type.- Specified by:
doubleStream
in interfaceDatabaseAccess
- Throws:
NullDataException
- When has a SQL NULL value.SQLException
-
intStream
public IntStream intStream(int isolationLevel, boolean readOnly, String sql, Object... params) throws NullDataException, SQLException Description copied from interface:DatabaseAccess
Query the database with anIntStream
return type.- Specified by:
intStream
in interfaceDatabaseAccess
- Throws:
NullDataException
- When has a SQL NULL value.SQLException
-
longStream
public LongStream longStream(int isolationLevel, boolean readOnly, String sql, Object... params) throws NullDataException, SQLException Description copied from interface:DatabaseAccess
Query the database with aLongStream
return type.- Specified by:
longStream
in interfaceDatabaseAccess
- Throws:
NullDataException
- When has a SQL NULL value.SQLException
-
stream
public <T,Ex extends Throwable> Stream<T> stream(int isolationLevel, boolean readOnly, Class<? extends Ex> exClass, ObjectFactoryE<? extends T, ? extends Ex> objectFactory, String sql, Object... params) throws SQLException, ExQuery the database with aStream<T>
return type, objects are created with the provided factory.When the factory is nullable, the stream may contain
null
elements. Otherwise, will have the characteristicSpliterator.NONNULL
.- Specified by:
stream
in interfaceDatabaseAccess
- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex
-
queryCall
public <T,Ex extends Throwable> T queryCall(int isolationLevel, boolean readOnly, Class<? extends Ex> exClass, ResultSetCallableE<? extends T, ? extends Ex> resultSetCallable, String sql, Object... params) throws SQLException, ExQuery the database, calling theResultSetCallableE
once.- Specified by:
queryCall
in interfaceDatabaseAccess
- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex
-
update
Description copied from interface:DatabaseAccess
Performs an update on the database and returns the number of rows affected.- Specified by:
update
in interfaceDatabaseAccess
- Throws:
SQLException
-
largeUpdate
Description copied from interface:DatabaseAccess
Performs an update on the database and returns the number of rows affected.- Specified by:
largeUpdate
in interfaceDatabaseAccess
- Throws:
SQLException
-
connect()
or one of the various call/run methods, which enforce the transaction semantics.