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
ConstructorsConstructorDescriptionDataSourceTransactionManager(@NonNull DataSource dataSource, ConnectionOperations<Connection> connectionOperations, @Nullable SynchronousConnectionManager<Connection> synchronousConnectionManager) Create a new DataSourceTransactionManager instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoBegin(DefaultTransactionStatus<Connection> status) Begin a new transaction with semantics according to the given transaction definition.protected voiddoCommit(DefaultTransactionStatus<Connection> status) Perform an actual commit of the given transaction.protected voidAlternative of theAbstractTransactionOperations.doBegin(InternalTransaction)intended for a nested transaction.protected voidAlternative of theAbstractTransactionOperations.doCommit(InternalTransaction)intended for a nested transaction.protected voidAlternative of theAbstractTransactionOperations.doRollback(InternalTransaction)intended for a nested transaction.protected voiddoRollback(DefaultTransactionStatus<Connection> status) Perform an actual rollback of the given transaction.@NonNull ConnectionObtains the connection for the current transaction.@NonNull DataSourcebooleanprotected voidprepareTransactionalConnection(Connection con, TransactionDefinition definition) Prepare the transactionalConnectionright after transaction begin.voidsetEnforceReadOnly(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, createNoTxTransactionStatusMethods inherited from class io.micronaut.transaction.support.AbstractTransactionOperations
commit, determineTimeout, doExecute, doResume, doSuspend, getConnectionDefinition, getTransaction, hasConnection, rollback, suspendMethods inherited from class io.micronaut.transaction.support.AbstractPropagatedStatusTransactionOperations
execute, extendCurrentPropagatedContext, findTransactionStatusMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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:AbstractTransactionOperationsBegin 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:
doBeginin classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection> - Parameters:
status- The transaction
-
doCommit
Description copied from class:AbstractTransactionOperationsPerform 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:
doCommitin classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection> - Parameters:
status- The transaction
-
doRollback
Description copied from class:AbstractTransactionOperationsPerform 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:
doRollbackin classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection> - Parameters:
status- The transaction
-
doNestedBegin
Description copied from class:AbstractTransactionOperationsAlternative of theAbstractTransactionOperations.doBegin(InternalTransaction)intended for a nested transaction.- Overrides:
doNestedBeginin classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection> - Parameters:
status- The transaction
-
doNestedCommit
Description copied from class:AbstractTransactionOperationsAlternative of theAbstractTransactionOperations.doCommit(InternalTransaction)intended for a nested transaction.- Overrides:
doNestedCommitin classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection> - Parameters:
status- The transaction
-
doNestedRollback
Description copied from class:AbstractTransactionOperationsAlternative of theAbstractTransactionOperations.doRollback(InternalTransaction)intended for a nested transaction.- Overrides:
doNestedRollbackin classAbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection> - Parameters:
status- The transaction
-
prepareTransactionalConnection
protected void prepareTransactionalConnection(Connection con, TransactionDefinition definition) throws SQLException Prepare the transactionalConnectionright after transaction begin.The default implementation executes a "SET TRANSACTION READ ONLY" statement if the
"enforceReadOnly"flag is set totrueand 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:TransactionOperationsObtains the connection for the current transaction.- Returns:
- The connection
-