- java.lang.Object
-
- com.aoapps.dbc.Database
-
- All Implemented Interfaces:
DatabaseAccess
- Direct Known Subclasses:
CloseableDatabase
public class Database extends Object implements DatabaseAccess
Wraps and simplifies access to a JDBC database. If used directly as aDatabaseAccess
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, useDatabaseConnection
.- Author:
- AO Industries, Inc.
- See Also:
createDatabaseConnection()
,DatabaseConnection
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.aoapps.dbc.DatabaseAccess
DatabaseAccess.Null
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
close()
Closes the database.DatabaseConnection
connect()
Creates a newDatabaseConnection
instance.DatabaseConnection
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.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
doubleStream(int isolationLevel, boolean readOnly, String sql, Object... params)
Query the database with aDoubleStream
return type.<V> V
executeTransaction(DatabaseCallable<V> callable)
Deprecated, for removal: This API element is subject to removal in a future version.void
executeTransaction(DatabaseRunnable runnable)
Deprecated, for removal: This API element is subject to removal in a future version.<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.<Ex extends Exception>
voidexecuteTransaction(Class<Ex> exClass, DatabaseRunnableE<Ex> runnable)
Deprecated, for removal: This API element is subject to removal in a future version.Connection
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.Connection
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.Connection
getConnection(int maxConnections)
Gets a read/write connection to the database with a transaction level ofConnections.DEFAULT_TRANSACTION_ISOLATION
.Connection
getConnection(int isolationLevel, boolean readOnly)
Gets a connection to the database, warning when a connection is already used by this thread.Connection
getConnection(int isolationLevel, boolean readOnly, int maxConnections)
Gets a connection to the database.protected FailFastConnection
getConnection(int isolationLevel, boolean readOnly, int maxConnections, boolean allowClose)
AOConnectionPool
getConnectionPool()
Gets the pool ornull
if using aDataSource
.DataSource
getDataSource()
Gets the data source ornull
if using anAOConnectionPool
.Logger
getLogger()
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()
.IntStream
intStream(int isolationLevel, boolean readOnly, String sql, Object... params)
Query the database with anIntStream
return type.boolean
isInTransaction()
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
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.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.<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)
Query the database with aStream<T>
return type, objects are created with the provided factory.String
toString()
<V> V
transactionCall(DatabaseCallable<? extends V> callable)
Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.<V> V
transactionCall(CallableE<? extends V,? extends SQLException> callable)
Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.<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.<V,Ex extends Throwable>
VtransactionCall(Class<? extends Ex> exClass, CallableE<? extends V,? extends Ex> callable)
Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.void
transactionRun(DatabaseRunnable runnable)
Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.void
transactionRun(RunnableE<? extends SQLException> runnable)
Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.<Ex extends Throwable>
voidtransactionRun(Class<? extends Ex> exClass, DatabaseRunnableE<? extends Ex> runnable)
Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.<Ex extends Throwable>
voidtransactionRun(Class<? extends Ex> exClass, RunnableE<? extends Ex> runnable)
Executes an arbitrary transaction, providing automatic commit, rollback, and connection management.int
update(String sql, Object... params)
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, queryBoolean, queryBoolean, queryByteArray, queryByteArray, queryCall, queryCall, queryCall, queryCollection, queryCollection, queryCollection, queryCollection, queryDate, queryDate, queryDouble, queryDouble, queryFloat, queryFloat, queryInt, queryInt, queryIntList, queryIntList, queryList, queryList, queryList, queryList, queryLong, queryLong, queryLongList, queryLongList, queryNewCollection, queryNewCollection, queryNewCollection, queryNewCollection, queryObject, queryObject, queryObject, queryObject, queryRun, queryRun, queryRun, queryRun, queryShort, queryShort, queryString, queryString, queryStringList, queryStringList, queryTimestamp, queryTimestamp, stream, stream, stream, streamOptional, streamOptional, streamOptional, streamOptional, streamOptionalUpdate, streamOptionalUpdate, streamString, streamString, streamStringUpdate, streamUpdate, streamUpdate, updateBigDecimal, updateBoolean, updateByteArray, updateCall, updateCall, updateCollection, updateCollection, updateDate, updateDouble, updateFloat, updateInt, updateIntList, updateList, updateList, updateLong, updateLongList, updateNewCollection, updateNewCollection, updateObject, updateObject, updateRun, updateRun, updateShort, updateString, updateStringList, updateTimestamp
-
-
-
-
Constructor Detail
-
Database
public Database(String driver, String url, String user, String password, int numConnections, long maxConnectionAge, Logger logger)
-
Database
public Database(AOConnectionPool pool)
-
Database
public Database(DataSource dataSource, Logger logger)
-
-
Method Detail
-
createDatabaseConnection
@Deprecated(forRemoval=true) public final DatabaseConnection 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
public DatabaseConnection 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:
transactionCall(com.aoapps.lang.concurrent.CallableE)
,transactionCall(java.lang.Class, com.aoapps.lang.concurrent.CallableE)
,transactionCall(com.aoapps.dbc.DatabaseCallable)
,transactionCall(java.lang.Class, com.aoapps.dbc.DatabaseCallableE)
,transactionRun(com.aoapps.lang.RunnableE)
,transactionRun(java.lang.Class, com.aoapps.lang.RunnableE)
,transactionRun(com.aoapps.dbc.DatabaseRunnable)
,transactionRun(java.lang.Class, com.aoapps.dbc.DatabaseRunnableE)
-
getConnectionPool
public AOConnectionPool getConnectionPool()
Gets the pool ornull
if using aDataSource
.- See Also:
getDataSource()
-
getDataSource
public DataSource getDataSource()
Gets the data source ornull
if using anAOConnectionPool
.- See Also:
getConnectionPool()
-
close
protected void close()
Closes the database.- See Also:
AOPool.close()
,Executors.close()
,CloseableDatabase.close()
-
initSqlDataTypes
protected void initSqlDataTypes(Connection conn) throws SQLException
Whenever a new connection is obtained from the pool or the dataSource, it is passed here for initialization ofgetSqlDataTypes()
.
-
deinitSqlDataTypes
protected void deinitSqlDataTypes(Connection conn) throws SQLException
Before a connection is release back to the pool or the dataSource, it is passed here for de-initialization ofgetSqlDataTypes()
.
-
initConnection
protected void initConnection(Connection conn) throws SQLException
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.
-
deinitConnection
protected void deinitConnection(Connection conn) throws SQLException
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.
-
getConnection
public Connection getConnection() throws SQLException
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(int, boolean, int, boolean)
,DatabaseConnection.getConnection()
,Connection.close()
-
getConnection
public Connection getConnection(int maxConnections) throws SQLException
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(int, boolean, int, boolean)
,DatabaseConnection.getConnection(int)
,Connection.close()
-
getConnection
public Connection getConnection(boolean readOnly) throws SQLException
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(int, boolean, int, boolean)
,DatabaseConnection.getConnection(boolean)
,Connection.close()
-
getConnection
public Connection getConnection(int isolationLevel, boolean readOnly) throws SQLException
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(int, boolean, int, boolean)
,DatabaseConnection.getConnection(int, boolean)
,Connection.close()
-
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:
DatabaseConnection.getConnection(int, boolean, int)
,Connection.close()
-
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
protected void release(FailFastConnection conn) throws SQLException
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).
-
getLogger
public Logger getLogger()
-
isInTransaction
public boolean isInTransaction()
Checks if the current thread is in a transaction.- See Also:
transactionCall(com.aoapps.lang.concurrent.CallableE)
,transactionCall(java.lang.Class, com.aoapps.lang.concurrent.CallableE)
,transactionCall(com.aoapps.dbc.DatabaseCallable)
,transactionCall(java.lang.Class, com.aoapps.dbc.DatabaseCallableE)
,transactionRun(com.aoapps.lang.RunnableE)
,transactionRun(java.lang.Class, com.aoapps.lang.RunnableE)
,transactionRun(com.aoapps.dbc.DatabaseRunnable)
,transactionRun(java.lang.Class, com.aoapps.dbc.DatabaseRunnableE)
-
transactionCall
public <V> V transactionCall(CallableE<? extends V,? extends SQLException> callable) throws SQLException
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.- Throws:
SQLException
- See Also:
isInTransaction()
- Rolls-back the transaction on
-
transactionCall
public <V,Ex extends Throwable> V transactionCall(Class<? extends Ex> exClass, CallableE<? extends V,? extends Ex> callable) throws SQLException, Ex extends Throwable
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.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex extends Throwable
- See Also:
isInTransaction()
- Rolls-back the transaction on
-
transactionCall
public <V> V transactionCall(DatabaseCallable<? extends V> callable) throws SQLException
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.- Throws:
SQLException
- See Also:
isInTransaction()
- Rolls-back the transaction on
-
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, Ex extends Throwable
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.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex extends Throwable
- See Also:
isInTransaction()
- 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, Ex extends Exception
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 extends Exception
-
transactionRun
public void transactionRun(RunnableE<? extends SQLException> runnable) throws SQLException
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.- Throws:
SQLException
- See Also:
isInTransaction()
- Rolls-back the transaction on
-
transactionRun
public <Ex extends Throwable> void transactionRun(Class<? extends Ex> exClass, RunnableE<? extends Ex> runnable) throws SQLException, Ex extends Throwable
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.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex extends Throwable
- See Also:
isInTransaction()
- Rolls-back the transaction on
-
transactionRun
public void transactionRun(DatabaseRunnable runnable) throws SQLException
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.- Throws:
SQLException
- See Also:
isInTransaction()
- Rolls-back the transaction on
-
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
-
transactionRun
public <Ex extends Throwable> void transactionRun(Class<? extends Ex> exClass, DatabaseRunnableE<? extends Ex> runnable) throws SQLException, Ex extends Throwable
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.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex extends Throwable
- See Also:
isInTransaction()
- Rolls-back the transaction on
-
executeTransaction
@Deprecated(forRemoval=true) public final <Ex extends Exception> void executeTransaction(Class<Ex> exClass, DatabaseRunnableE<Ex> runnable) throws SQLException, Ex extends Exception
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 extends Exception
-
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
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
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
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, Ex extends Throwable
Query 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 extends Throwable
-
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, Ex extends Throwable
Query the database, calling theResultSetCallableE
once.- Specified by:
queryCall
in interfaceDatabaseAccess
- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
SQLException
Ex extends Throwable
-
update
public int update(String sql, Object... params) throws SQLException
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
public long largeUpdate(String sql, Object... params) throws SQLException
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
-
-