Example usage for org.springframework.transaction TransactionDefinition TIMEOUT_DEFAULT

List of usage examples for org.springframework.transaction TransactionDefinition TIMEOUT_DEFAULT

Introduction

In this page you can find the example usage for org.springframework.transaction TransactionDefinition TIMEOUT_DEFAULT.

Prototype

int TIMEOUT_DEFAULT

To view the source code for org.springframework.transaction TransactionDefinition TIMEOUT_DEFAULT.

Click Source Link

Document

Use the default timeout of the underlying transaction system, or none if timeouts are not supported.

Usage

From source file:pl.com.bottega.testutils.HibernateExtendedJpaDialect.java

/**
 * This method is overridden to set custom isolation levels on the connection
 * @param entityManager//w w  w .j  a  v  a 2  s .  co m
 * @param definition
 * @return
 * @throws PersistenceException
 * @throws SQLException
 * @throws TransactionException
 */
@Override
public Object beginTransaction(final EntityManager entityManager, final TransactionDefinition definition)
        throws PersistenceException, SQLException, TransactionException {
    Session session = (Session) entityManager.getDelegate();
    if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
        getSession(entityManager).getTransaction().setTimeout(definition.getTimeout());
    }

    entityManager.getTransaction().begin();
    logger.debug("Transaction started");

    session.doWork(new Work() {

        public void execute(Connection connection) throws SQLException {
            logger.debug("The connection instance is {}", connection);
            logger.debug(
                    "The isolation level of the connection is {} and the isolation level set on the transaction is {}",
                    connection.getTransactionIsolation(), definition.getIsolationLevel());
            DataSourceUtils.prepareConnectionForTransaction(connection, definition);
        }
    });

    return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName());
}

From source file:org.alfresco.util.transaction.SpringAwareUserTransactionTest.java

private UserTransaction getTxn() {
    return new SpringAwareUserTransaction(transactionManager, false, TransactionDefinition.ISOLATION_DEFAULT,
            TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.TIMEOUT_DEFAULT);
}

From source file:com.haulmont.cuba.core.sys.CubaEclipseLinkJpaDialect.java

@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
        throws PersistenceException, SQLException, TransactionException {
    Object result = super.beginTransaction(entityManager, definition);
    Preconditions.checkState(result == null, "Transactional data should be null for EclipseLink dialect");

    // Read default timeout every time - may be somebody wants to change it on the fly
    int defaultTimeout = 0;
    String defaultTimeoutProp = AppContext.getProperty("cuba.defaultQueryTimeoutSec");
    if (!"0".equals(defaultTimeoutProp) && !StringUtils.isBlank(defaultTimeoutProp)) {
        try {/*w  w w . j  a  v  a2  s  . c  o m*/
            defaultTimeout = Integer.parseInt(defaultTimeoutProp);
        } catch (NumberFormatException e) {
            log.error("Invalid cuba.defaultQueryTimeoutSec value", e);
        }
    }

    int timeoutSec = 0;
    if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT)
        timeoutSec = definition.getTimeout();
    else if (defaultTimeout != 0)
        timeoutSec = defaultTimeout;

    if (timeoutSec != 0) {
        log.trace("Applying query timeout " + timeoutSec + "sec");
        if (entityManager instanceof JpaEntityManager) {
            UnitOfWork unitOfWork = ((JpaEntityManager) entityManager).getUnitOfWork();
            if (unitOfWork != null) {
                //setup delay in seconds on unit of work
                unitOfWork.setQueryTimeoutDefault(timeoutSec);
            }
        }

        String s = DbmsSpecificFactory.getDbmsFeatures().getTransactionTimeoutStatement();
        if (s != null) {
            Connection connection = entityManager.unwrap(Connection.class);
            try (Statement statement = connection.createStatement()) {
                statement.execute(String.format(s, timeoutSec * 1000));
            }
        }
    }

    return new CubaEclipseLinkTransactionData(entityManager);
}

From source file:org.springextensions.neodatis.NeoDatisTransactionManager.java

@Override
protected void doBegin(Object transaction, TransactionDefinition transactionDefinition)
        throws TransactionException {
    if (transactionDefinition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new InvalidIsolationLevelException("NeoDatis does not support an isolation level concept.");
    }//from  www.  j  a va 2 s.co  m

    ODB odb = null;

    try {
        NeoDatisTransactionObject transactionObject = (NeoDatisTransactionObject) transaction;
        if (transactionObject.getODBHolder() == null) {
            odb = getOdb();
            logger.debug("Using given ODB [{}] for the current thread transaction.", odb);
            transactionObject.setODBHolder(new ODBHolder(odb));
        }

        ODBHolder odbHolder = transactionObject.getODBHolder();

        odbHolder.setSynchronizedWithTransaction(true);

        // start transaction
        // no-op
        // register timeout
        if (transactionDefinition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
            transactionObject.getODBHolder().setTimeoutInSeconds(transactionDefinition.getTimeout());
        }

        //Bind session holder to the thread
        TransactionSynchronizationManager.bindResource(getOdb(), odbHolder);

    } catch (Exception e) {
        throw new CannotCreateTransactionException("Can not create an NeoDatis transaction.", e);
    }

}

From source file:org.grails.datastore.mapping.transactions.DatastoreTransactionManager.java

@Override
protected void doBegin(Object o, TransactionDefinition definition) throws TransactionException {
    TransactionObject txObject = (TransactionObject) o;

    Session session = null;//from   w  w w  . j ava  2s  .  c  o  m
    try {
        session = txObject.getSessionHolder().getSession();

        if (definition.isReadOnly()) {
            // Just set to NEVER in case of a new Session for this transaction.
            session.setFlushMode(FlushModeType.COMMIT);
        }

        Transaction<?> tx;
        // Register transaction timeout.
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            tx = session.beginTransaction();
            tx.setTimeout(timeout);
        } else {
            // Open a plain Datastore transaction without specified timeout.
            tx = session.beginTransaction();
        }

        // Add the Datastore transaction to the session holder.
        txObject.setTransaction(tx);

        // Bind the session holder to the thread.
        if (txObject.isNewSessionHolder()) {
            TransactionSynchronizationManager.bindResource(getDatastore(), txObject.getSessionHolder());
        }
        txObject.getSessionHolder().setSynchronizedWithTransaction(true);
    } catch (Exception ex) {
        if (txObject.isNewSession()) {
            try {
                if (session != null && session.getTransaction().isActive()) {
                    session.getTransaction().rollback();
                }
            } catch (Throwable ex2) {
                logger.debug("Could not rollback Session after failed transaction begin", ex);
            } finally {
                DatastoreUtils.closeSession(session);
            }
        }
        throw new CannotCreateTransactionException("Could not open Datastore Session for transaction", ex);
    }
}

From source file:org.springextensions.db4o.Db4oTransactionManager.java

protected void doBegin(Object transaction, TransactionDefinition transactionDefinition)
        throws TransactionException {
    if (transactionDefinition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new InvalidIsolationLevelException("Db4o does not support an isolation level concept");
    }// w  w w .j a  va  2  s  . co m

    ObjectContainer container = null;

    try {
        Db4oTransactionObject txObject = (Db4oTransactionObject) transaction;
        if (txObject.getObjectContainerHolder() == null) {
            // use the given container
            container = getObjectContainer();
            logger.debug("Using given objectContainer [{}] for the current thread transaction", container);
            txObject.setObjectContainerHolder(new ObjectContainerHolder(container));
        }

        ObjectContainerHolder containerHolder = txObject.getObjectContainerHolder();

        containerHolder.setSynchronizedWithTransaction(true);

        /*
        * We have no notion of flushing inside a db4o object container
        *
        if (transactionDefinition.isReadOnly() && txObject.isNewObjectContainerHolder()) {
        containerHolder.setReadOnly(true);
        }
                
        if (!transactionDefinition.isReadOnly() && !txObject.isNewObjectContainerHolder()) {
        if (containerHolder.isReadOnly()) {
        containerHolder.setReadOnly(false);
        }
        }
        */

        // start the transaction
        // no-op
        // Register transaction timeout.
        if (transactionDefinition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getObjectContainerHolder().setTimeoutInSeconds(transactionDefinition.getTimeout());
        }

        // Bind the session holder to the thread.
        TransactionSynchronizationManager.bindResource(getObjectContainer(), containerHolder);
    } catch (Exception ex) {
        throw new CannotCreateTransactionException("Could not start db4o object container transaction", ex);
    }
}

From source file:com.turbospaces.spaces.tx.SpaceTransactionManager.java

@Override
public void afterPropertiesSet() {
    setNestedTransactionAllowed(false);/* w  w w  . java  2s  . co m*/
    Preconditions.checkNotNull(jSpace, "SpaceTransactionManager can't be initialized: 'jSpace' is missing");

    {
        // set default transaction timeout implicitly in seconds
        if (getDefaultTimeout() == TransactionDefinition.TIMEOUT_DEFAULT) {
            log.info("setting default transaction timeout implicitly to {} seconds",
                    AbstractSpaceConfiguration.defaultTransactionTimeout());
            setDefaultTimeout(AbstractSpaceConfiguration.defaultTransactionTimeout());
        }
    }
}

From source file:org.alfresco.util.transaction.SpringAwareUserTransactionTest.java

private UserTransaction getFailingTxn() {
    return new SpringAwareUserTransaction(failingTransactionManager, false,
            TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRED,
            TransactionDefinition.TIMEOUT_DEFAULT);
}

From source file:org.cfr.capsicum.datasource.CayenneTransactionManager.java

/**
 * This implementation sets the isolation level but ignores the timeout.
 *//*  w ww .  j  a va  2  s.  c o m*/
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    CayenneTransactionObject txObject = (CayenneTransactionObject) transaction;
    Connection con = null;

    try {
        if (txObject.getConnectionHolder() == null
                || txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
            Connection newCon = this.dataSource.getConnection();
            if (logger.isDebugEnabled()) {
                logger.debug("Acquired Connection [" + newCon + "] for JDBC transaction");
            }
            ObjectContext context = null;
            if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW) {
                Injector injector = this.cayenneRuntime.getInjector();
                context = injector.getInstance(ObjectContextFactory.class).createContext();
            } else {
                context = BaseContext.getThreadObjectContext();
            }
            txObject.setConnectionHolder(new CayenneConnectionHolder(newCon, context), true);
            BaseContext.bindThreadObjectContext(context);
        }

        txObject.getConnectionHolder().setSynchronizedWithTransaction(true);
        con = txObject.getConnectionHolder().getConnection();

        Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
        txObject.setPreviousIsolationLevel(previousIsolationLevel);

        // Switch to manual commit if necessary. This is very expensive in
        // some
        // JDBC drivers,
        // so we don't want to do it unnecessarily (for example if we've
        // explicitly
        // 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);
        }
        txObject.getConnectionHolderEx().setTransactionActive(true);

        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getConnectionHolder().setTimeoutInSeconds(timeout);
        }

        // Bind the session holder to the thread.
        if (txObject.isNewConnectionHolder()) {
            TransactionSynchronizationManager.bindResource(getDataSource(), txObject.getConnectionHolder());
        }
    }

    catch (SQLException ex) {
        DataSourceUtils.releaseConnection(con, this.dataSource);
        throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction",
                exceptionTranslator.convertJdbcAccessException(ex));
    }
}

From source file:org.guzz.web.context.spring.GuzzTransactionManager.java

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    GuzzTransactionObject txObject = (GuzzTransactionObject) transaction;

    //TODO: checkout the outside DataSourceTransactionManager 
    //      if (txObject.hasConnectionHolder() && !txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
    //         throw new IllegalTransactionStateException(
    //               "Pre-bound JDBC Connection found! GuzzTransactionManager does not support " +
    //               "running within DataSourceTransactionManager if told to manage the DataSource itself. ") ;
    //      }//from w w w  .j a  v  a 2s . co m

    WriteTranSession writeTranSession = null;

    try {
        if (txObject.getSessionHolder() == null
                || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
            writeTranSession = getTransactionManager().openRWTran(false);

            if (logger.isDebugEnabled()) {
                logger.debug("Opened new Session [" + TransactionManagerUtils.toString(writeTranSession)
                        + "] for Guzz transaction");
            }
            txObject.setWriteTranSession(writeTranSession);
        }

        writeTranSession = txObject.getSessionHolder().getWriteTranSession();

        if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
            // We should set a specific isolation level but are not allowed to...
            IsolationsSavePointer oldSavePointer = writeTranSession
                    .setTransactionIsolation(definition.getIsolationLevel());

            txObject.setIsolationsSavePointer(oldSavePointer);
        }

        // Register transaction timeout.
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getSessionHolder().setTimeoutInSeconds(timeout);
        }

        // Bind the session holder to the thread.
        if (txObject.isNewWriteTranSessionHolder()) {
            TransactionSynchronizationManager.bindResource(getTransactionManager(),
                    txObject.getSessionHolder());
        }
        txObject.getSessionHolder().setSynchronizedWithTransaction(true);

    } catch (Exception ex) {
        if (txObject.isNewWriteTranSession()) {
            try {
                if (writeTranSession != null) {
                    //TransactionIsolation?????
                    writeTranSession.rollback();
                }
            } catch (Throwable ex2) {
                logger.debug("Could not rollback WriteTranSession after failed transaction begin", ex);
            } finally {
                TransactionManagerUtils.closeSession(writeTranSession);
            }
        }
        throw new CannotCreateTransactionException("Could not open Guzz WriteTranSession for transaction", ex);
    }
}