Class DataSourceTransactionManager
- All Implemented Interfaces:
SynchronousTransactionManager<Connection>
,TransactionManager
,TransactionOperations<Connection>
DataSource
transaction manager.
Partially based on https://github.com/spring-projects/spring-framework/blob/main/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java- Since:
- 4.0.0
- Author:
- Denis Stepanov
-
Field Summary
Fields inherited from class io.micronaut.transaction.support.AbstractTransactionOperations
connectionOperations, logger, synchronousConnectionManager
-
Constructor Summary
ConstructorDescriptionDataSourceTransactionManager
(@NonNull DataSource dataSource, ConnectionOperations<Connection> connectionOperations, @Nullable SynchronousConnectionManager<Connection> synchronousConnectionManager) Create a new DataSourceTransactionManager instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doBegin
(DefaultTransactionStatus<Connection> status) Begin a new transaction with semantics according to the given transaction definition.protected void
doCommit
(DefaultTransactionStatus<Connection> status) Perform an actual commit of the given transaction.protected void
Alternative of theAbstractTransactionOperations.doBegin(InternalTransaction)
intended for a nested transaction.protected void
Alternative of theAbstractTransactionOperations.doCommit(InternalTransaction)
intended for a nested transaction.protected void
Alternative of theAbstractTransactionOperations.doRollback(InternalTransaction)
intended for a nested transaction.protected void
doRollback
(DefaultTransactionStatus<Connection> status) Perform an actual rollback of the given transaction.@NonNull Connection
Obtains the connection for the current transaction.@NonNull DataSource
boolean
protected void
prepareTransactionalConnection
(Connection con, TransactionDefinition definition) Prepare the transactionalConnection
right after transaction begin.void
setEnforceReadOnly
(boolean enforceReadOnly) Specify whether to enforce the read-only nature of a transaction (as indicated byTransactionDefinition.isReadOnly()
through an explicit statement on the transactional connection: "SET TRANSACTION READ ONLY" as understood by Oracle, MySQL and Postgres.Methods inherited from class io.micronaut.transaction.support.AbstractDefaultTransactionOperations
createExistingTransactionStatus, createNewTransactionStatus, createNoTxTransactionStatus
Methods inherited from class io.micronaut.transaction.support.AbstractTransactionOperations
commit, determineTimeout, doExecute, doResume, doSuspend, getConnectionDefinition, getTransaction, hasConnection, rollback, suspend
Methods inherited from class io.micronaut.transaction.support.AbstractPropagatedStatusTransactionOperations
execute, extendCurrentPropagatedContext, findTransactionStatus
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.micronaut.transaction.TransactionOperations
execute, executeRead, executeWrite, findTransactionStatus
-
Constructor Details
-
DataSourceTransactionManager
public DataSourceTransactionManager(@NonNull @NonNull DataSource dataSource, @Parameter ConnectionOperations<Connection> connectionOperations, @Parameter @Nullable @Nullable SynchronousConnectionManager<Connection> synchronousConnectionManager) Create a new DataSourceTransactionManager instance.- Parameters:
dataSource
- the JDBC DataSource to manage transactions forconnectionOperations
- the connection operationssynchronousConnectionManager
- the synchronous connection operations
-
-
Method Details
-
getDataSource
- Returns:
- Return the JDBC DataSource that this instance manages transactions for.
-
setEnforceReadOnly
public void setEnforceReadOnly(boolean enforceReadOnly) Specify whether to enforce the read-only nature of a transaction (as indicated byTransactionDefinition.isReadOnly()
through an explicit statement on the transactional connection: "SET TRANSACTION READ ONLY" as understood by Oracle, MySQL and Postgres.The exact treatment, including any SQL statement executed on the connection, can be customized through
prepareTransactionalConnection(java.sql.Connection, io.micronaut.transaction.TransactionDefinition)
.This mode of read-only handling goes beyond the
Connection.setReadOnly(boolean)
hint that Spring applies by default. In contrast to that standard JDBC hint, "SET TRANSACTION READ ONLY" enforces an isolation-level-like connection mode where data manipulation statements are strictly disallowed. Also, on Oracle, this read-only mode provides read consistency for the entire transaction.Note that older Oracle JDBC drivers (9i, 10g) used to enforce this read-only mode even for
Connection.setReadOnly(true
. However, with recent drivers, this strong enforcement needs to be applied explicitly, e.g. through this flag.- Parameters:
enforceReadOnly
- True if read-only should be enforced- Since:
- 4.3.7
- See Also:
-
isEnforceReadOnly
public boolean isEnforceReadOnly()- Returns:
- Return whether to enforce the read-only nature of a transaction through an explicit statement on the transactional connection.
- Since:
- 4.3.7
- See Also:
-
doBegin
Description copied from class:AbstractTransactionOperations
Begin a new transaction with semantics according to the given transaction definition. Does not have to care about applying the propagation behavior, as this has already been handled by this abstract manager.This method gets called when the transaction manager has decided to actually start a new transaction. Either there wasn't any transaction before, or the previous transaction has been suspended.
- Specified by:
doBegin
in classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,
Connection> - Parameters:
status
- The transaction
-
doCommit
Description copied from class:AbstractTransactionOperations
Perform an actual commit of the given transaction.An implementation does not need to check the "new transaction" flag or the rollback-only flag; this will already have been handled before. Usually, a straight commit will be performed on the transaction object contained in the passed-in status.
- Specified by:
doCommit
in classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,
Connection> - Parameters:
status
- The transaction
-
doRollback
Description copied from class:AbstractTransactionOperations
Perform an actual rollback of the given transaction.An implementation does not need to check the "new transaction" flag; this will already have been handled before. Usually, a straight rollback will be performed on the transaction object contained in the passed-in status.
- Specified by:
doRollback
in classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,
Connection> - Parameters:
status
- The transaction
-
doNestedBegin
Description copied from class:AbstractTransactionOperations
Alternative of theAbstractTransactionOperations.doBegin(InternalTransaction)
intended for a nested transaction.- Overrides:
doNestedBegin
in classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,
Connection> - Parameters:
status
- The transaction
-
doNestedCommit
Description copied from class:AbstractTransactionOperations
Alternative of theAbstractTransactionOperations.doCommit(InternalTransaction)
intended for a nested transaction.- Overrides:
doNestedCommit
in classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,
Connection> - Parameters:
status
- The transaction
-
doNestedRollback
Description copied from class:AbstractTransactionOperations
Alternative of theAbstractTransactionOperations.doRollback(InternalTransaction)
intended for a nested transaction.- Overrides:
doNestedRollback
in classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,
Connection> - Parameters:
status
- The transaction
-
prepareTransactionalConnection
protected void prepareTransactionalConnection(Connection con, TransactionDefinition definition) throws SQLException Prepare the transactionalConnection
right after transaction begin.The default implementation executes a "SET TRANSACTION READ ONLY" statement if the
"enforceReadOnly"
flag is set totrue
and the transaction definition indicates a read-only transaction.The "SET TRANSACTION READ ONLY" is understood by Oracle, MySQL and Postgres and may work with other databases as well. If you'd like to adapt this treatment, override this method accordingly.
- Parameters:
con
- the transactional JDBC Connectiondefinition
- the current transaction definition- Throws:
SQLException
- if thrown by JDBC API- Since:
- 4.3.7
- See Also:
-
getConnection
Description copied from interface:TransactionOperations
Obtains the connection for the current transaction.- Returns:
- The connection
-