public class DatabaseAccessorImpl extends Object implements DatabaseAccessor
Constructor and Description |
---|
DatabaseAccessorImpl(DatabaseConfigurationManager databaseConfigurationManager) |
Modifier and Type | Method and Description |
---|---|
<R> R |
executeQuery(ConnectionFunction<R> callback)
Executes SQL statements as defined in the callback function.
|
String |
getDatabaseType()
Returns the database type that we are connected to.
|
DatabaseVendor |
getDatabaseVendor()
Returns the vendor of the database that we are connected to.
|
Optional<String> |
getSchemaName()
Returns the configured schema name of the configured database connection.
|
String |
getVersion()
Theoretically nullable, but the meaning of if it is null is undefined
(depends on the database metadata implementation)
|
<R> Optional<R> |
runInManagedOptionalAwareTransaction(java.util.function.Function<Connection,Optional<R>> callback)
Executes SQL statements as defined in the callback function and manages transaction semantics.
|
<R> R |
runInManagedTransaction(java.util.function.Function<Connection,R> callback)
Executes SQL statements as defined in the callback function and manages transaction semantics.
|
<R> R |
runInTransaction(java.util.function.Function<Connection,R> callback)
Executes SQL statements as defined in the callback function and manages transaction semantics.
|
public DatabaseAccessorImpl(DatabaseConfigurationManager databaseConfigurationManager)
@Nonnull public DatabaseVendor getDatabaseVendor()
DatabaseAccessor
getDatabaseVendor
in interface DatabaseAccessor
@Nonnull public String getDatabaseType()
DatabaseAccessor
getDatabaseType
in interface DatabaseAccessor
@Nonnull public Optional<String> getSchemaName()
DatabaseAccessor
getSchemaName
in interface DatabaseAccessor
public <R> R executeQuery(@Nonnull ConnectionFunction<R> callback)
DatabaseAccessor
This method will borrow a new connection from the pool, pass it to the callback function and then return it to
the pool after the callback has completed.
Even if OfBiz is currently running in a ThreadLocal transaction, this will retrieve a fresh connection from
the pool.
If you want to run in an existing OfBiz transaction then see instead DatabaseAccessor.runInTransaction(Function)
or if
want to ensure a transaction is started if not already existing, see DatabaseAccessor.runInManagedTransaction(Function)
or
DatabaseAccessor.runInManagedOptionalAwareTransaction(Function)
Because database connections are limited resources, your code should do its database operations and return from the callback as soon as possible in order to not starve other threads of this resource. In particular, you should not call any code that may in turn borrow a second connection from the pool (this can lead to a deadlock). In general avoid doing anything "slow" (eg I/O) nor await on other limited resources (eg locks).
Do not close the given connection - DatabaseAccessor will return it to the pool for you after the method is complete. If the ConnectionFunction callback throws a RuntimeException and the connection is not in auto-commit mode, then this method will perform a rollback on the connection (no explicit rollback is required).
Do not hold onto the Connection after your callback completes. Once the callback completes, the connection will be returned to the pool and can be immediately borrowed by another thread.
The connection will have the default auto-commit value as defined by the JIRA connection pool.
As at JIRA 7.0 this means autocommit == true.
See org.apache.commons.dbcp.PoolableConnectionFactory#activateObject(Object)
for details.
Note that this is very different to the behaviour of DatabaseAccessor.runInTransaction(Function)
where the
transaction is completely managed for you.
Example Usage for autocommit:
databaseAccessor.execute(connection -> runMyQuery(connection.getJdbcConnection()));
Example Usage with transaction:
databaseAccessor.executeQuery( connection -> { connection.setAutoCommit(false); // throws RuntimeException to indicate rollback doSomeUpdates(connection.getJdbcConnection()); connection.commit(); } );
executeQuery
in interface DatabaseAccessor
R
- the return type of the callback and of this methodcallback
- the callback function that can run one or more queries with the managed connection.public <R> R runInTransaction(@Nonnull java.util.function.Function<Connection,R> callback)
DatabaseAccessor
This method will attempt to run the callback within an existing OfBiz transaction if one is running within this thread. For that reason, the commit and rollback will be completely managed for you. If the callback returns successfully, then this indicates that the underlying transaction is allowed to be committed at the appropriate time. If you want to rollback the transaction then throw a RuntimeException. The transaction will be marked as "rollback required" such that the code that started the transaction will eventually call rollback on the Connection and the exception will be propagated.
Because the connection (borrowing and returning from the pool) and its transaction are managed for you,
then specific Connection
methods are illegal to call and will cause a RuntimeException including:
Connection.setAutoCommit(boolean)
autocommit is always falseConnection.commit()
Connection.rollback()
Connection.close()
DatabaseAccessor.executeQuery(ConnectionFunction)
where the
transaction is not managed.
Example Usage:
databaseAccessor.runInTransaction( connection -> { // throws RuntimeException to indicate rollback required doSomeUpdates(connection); } );
runInTransaction
in interface DatabaseAccessor
R
- the return type of the callback and of this methodcallback
- the callback function that can run one or more queries with the managed connection.public <R> R runInManagedTransaction(@Nonnull java.util.function.Function<Connection,R> callback)
DatabaseAccessor
This method will attempt to run the callback within an existing OfBiz transaction if one is running within this thread, or begin a new OfBiz transaction if one is not already running.
For that reason, the commit and rollback will be completely managed for you.
If the callback returns successfully, then this indicates that the underlying transaction is allowed to be committed at the appropriate time.
If you want to rollback the transaction then throw a RuntimeException. The transaction will be marked as "rollback required" such that the code that started the transaction will eventually call rollback on the Connection and the exception will be propagated.
Because the connection (borrowing and returning from the pool) and its transaction are managed for you,
then specific Connection
methods are illegal to call and will cause a RuntimeException including:
Connection.setAutoCommit(boolean)
autocommit is always falseConnection.commit()
Connection.rollback()
Connection.close()
DatabaseAccessor.executeQuery(ConnectionFunction)
where the
transaction is not managed.
Example Usage:
databaseAccessor.runInManagedTransaction( connection -> { // throws RuntimeException to indicate rollback required return doSomeUpdates(connection); } );
runInManagedTransaction
in interface DatabaseAccessor
R
- the return type of the callback and of this methodcallback
- the callback function that can run one or more queries with the managed connection.@Nonnull public <R> Optional<R> runInManagedOptionalAwareTransaction(@Nonnull java.util.function.Function<Connection,Optional<R>> callback)
DatabaseAccessor
This method will attempt to run the callback within an existing OfBiz transaction if one is running within this thread, or begin a new OfBiz transaction if one is not already running.
For that reason, the commit and rollback will be completely managed for you.
If the callback returns successfully, with an Optional.isPresent()
, then this indicates that the underlying
transaction is allowed to be committed at the appropriate time.
If you want to rollback the transaction then throw a RuntimeException or return Optional.empty()
. The
transaction will be marked as "rollback required" such that the code that started the transaction will eventually
call rollback on the Connection and the exception will be propagated.
Because the connection (borrowing and returning from the pool) and its transaction are managed for you,
then specific Connection
methods are illegal to call and will cause a RuntimeException including:
Connection.setAutoCommit(boolean)
autocommit is always falseConnection.commit()
Connection.rollback()
Connection.close()
DatabaseAccessor.executeQuery(ConnectionFunction)
where the
transaction is not managed.
Example Usage:
databaseAccessor.runInManagedOptionalAwareTransaction( connection -> { // throws RuntimeException or returns Optional.empty() to indicate rollback required return doSomeUpdates(connection); } );
runInManagedOptionalAwareTransaction
in interface DatabaseAccessor
R
- the return type of the callback and of this methodcallback
- the callback function that can run one or more queries with the managed connection.
Optional
result must not be nullpublic String getVersion()
DatabaseAccessor
getVersion
in interface DatabaseAccessor
DatabaseMetaData.getDatabaseProductVersion()
Copyright © 2002-2019 Atlassian. All Rights Reserved.