Class DataSourceTransactionManager

All Implemented Interfaces:
SynchronousTransactionManager<Connection>, TransactionManager, TransactionOperations<Connection>

@Internal @EachBean(javax.sql.DataSource.class) @Requires(condition=io.micronaut.transaction.jdbc.JdbcTransactionManagerCondition.class) public final class DataSourceTransactionManager extends AbstractDefaultTransactionOperations<Connection>
The 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
  • 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 for
      connectionOperations - the connection operations
      synchronousConnectionManager - the synchronous connection operations
  • Method Details

    • getDataSource

      @NonNull public @NonNull DataSource 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 by TransactionDefinition.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

      protected void doBegin(DefaultTransactionStatus<Connection> status)
      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 class AbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection>
      Parameters:
      status - The transaction
    • doCommit

      protected void doCommit(DefaultTransactionStatus<Connection> status)
      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 class AbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection>
      Parameters:
      status - The transaction
    • doRollback

      protected void doRollback(DefaultTransactionStatus<Connection> status)
      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 class AbstractTransactionOperations<DefaultTransactionStatus<Connection>,Connection>
      Parameters:
      status - The transaction
    • prepareTransactionalConnection

      protected void prepareTransactionalConnection(Connection con, TransactionDefinition definition) throws SQLException
      Prepare the transactional Connection right after transaction begin.

      The default implementation executes a "SET TRANSACTION READ ONLY" statement if the "enforceReadOnly" flag is set to true 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 Connection
      definition - the current transaction definition
      Throws:
      SQLException - if thrown by JDBC API
      Since:
      4.3.7
      See Also:
    • getConnection

      @NonNull public @NonNull Connection getConnection()
      Description copied from interface: TransactionOperations
      Obtains the connection for the current transaction.
      Returns:
      The connection