Search in sources :

Example 6 with TransactionDefinition

use of io.micronaut.transaction.TransactionDefinition in project micronaut-data by micronaut-projects.

the class DataSourceTransactionManager method doBegin.

/**
 * This implementation sets the isolation level but ignores the timeout.
 */
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
    Connection con = null;
    try {
        if (!txObject.hasConnectionHolder() || txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
            Connection newCon = dataSource.getConnection();
            if (logger.isDebugEnabled()) {
                logger.debug("Acquired Connection [" + newCon + "] for JDBC transaction");
            }
            txObject.setConnectionHolder(new ConnectionHolder(newCon), true);
        }
        txObject.getConnectionHolder().setSynchronizedWithTransaction(true);
        con = txObject.getConnectionHolder().getConnection();
        TransactionDefinition.Isolation previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
        txObject.setPreviousIsolationLevel(previousIsolationLevel);
        // configured the connection pool to set it already).
        if (con.getAutoCommit()) {
            txObject.setMustRestoreAutoCommit(true);
            if (logger.isDebugEnabled()) {
                logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
            }
            con.setAutoCommit(false);
        }
        prepareTransactionalConnection(con, definition);
        txObject.getConnectionHolder().setTransactionActive(true);
        Duration timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getConnectionHolder().setTimeout(timeout);
        }
        // Bind the connection holder to the thread.
        if (txObject.isNewConnectionHolder()) {
            TransactionSynchronizationManager.bindResource(dataSource, txObject.getConnectionHolder());
        }
    } catch (Throwable ex) {
        if (txObject.isNewConnectionHolder()) {
            DataSourceUtils.releaseConnection(con, dataSource);
            txObject.setConnectionHolder(null, false);
        }
        throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);
    }
}
Also used : TransactionDefinition(io.micronaut.transaction.TransactionDefinition) CannotCreateTransactionException(io.micronaut.transaction.exceptions.CannotCreateTransactionException) Connection(java.sql.Connection) Duration(java.time.Duration)

Aggregations

TransactionDefinition (io.micronaut.transaction.TransactionDefinition)6 Connection (java.sql.Connection)4 EachBean (io.micronaut.context.annotation.EachBean)2 Parameter (io.micronaut.context.annotation.Parameter)2 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 Internal (io.micronaut.core.annotation.Internal)2 NonNull (io.micronaut.core.annotation.NonNull)2 Nullable (io.micronaut.core.annotation.Nullable)2 BeanProperty (io.micronaut.core.beans.BeanProperty)2 ConversionContext (io.micronaut.core.convert.ConversionContext)2 Argument (io.micronaut.core.type.Argument)2 DataAccessException (io.micronaut.data.exceptions.DataAccessException)2 DataType (io.micronaut.data.model.DataType)2 Page (io.micronaut.data.model.Page)2 Dialect (io.micronaut.data.model.query.builder.sql.Dialect)2 AttributeConverterRegistry (io.micronaut.data.model.runtime.AttributeConverterRegistry)2 DeleteBatchOperation (io.micronaut.data.model.runtime.DeleteBatchOperation)2 DeleteOperation (io.micronaut.data.model.runtime.DeleteOperation)2 InsertBatchOperation (io.micronaut.data.model.runtime.InsertBatchOperation)2 InsertOperation (io.micronaut.data.model.runtime.InsertOperation)2