Search in sources :

Example 1 with InvalidIsolationLevelException

use of org.springframework.transaction.InvalidIsolationLevelException in project spring-framework by spring-projects.

the class JmsTransactionManager method doBegin.

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new InvalidIsolationLevelException("JMS does not support an isolation level concept");
    }
    JmsTransactionObject txObject = (JmsTransactionObject) transaction;
    Connection con = null;
    Session session = null;
    try {
        con = createConnection();
        session = createSession(con);
        if (logger.isDebugEnabled()) {
            logger.debug("Created JMS transaction on Session [" + session + "] from Connection [" + con + "]");
        }
        txObject.setResourceHolder(new JmsResourceHolder(getConnectionFactory(), con, session));
        txObject.getResourceHolder().setSynchronizedWithTransaction(true);
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getResourceHolder().setTimeoutInSeconds(timeout);
        }
        TransactionSynchronizationManager.bindResource(getConnectionFactory(), txObject.getResourceHolder());
    } catch (Throwable ex) {
        if (session != null) {
            try {
                session.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        throw new CannotCreateTransactionException("Could not create JMS transaction", ex);
    }
}
Also used : CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Connection(javax.jms.Connection) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) Session(javax.jms.Session)

Example 2 with InvalidIsolationLevelException

use of org.springframework.transaction.InvalidIsolationLevelException in project spring-framework by spring-projects.

the class HibernateTransactionManager method doBegin.

@Override
@SuppressWarnings("deprecation")
protected void doBegin(Object transaction, TransactionDefinition definition) {
    HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
    if (txObject.hasConnectionHolder() && !txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
        throw new IllegalTransactionStateException("Pre-bound JDBC Connection found! HibernateTransactionManager does not support " + "running within DataSourceTransactionManager if told to manage the DataSource itself. " + "It is recommended to use a single HibernateTransactionManager for all transactions " + "on a single DataSource, no matter whether Hibernate or JDBC access.");
    }
    Session session = null;
    try {
        if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
            Interceptor entityInterceptor = getEntityInterceptor();
            Session newSession = (entityInterceptor != null ? getSessionFactory().withOptions().interceptor(entityInterceptor).openSession() : getSessionFactory().openSession());
            if (logger.isDebugEnabled()) {
                logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
            }
            txObject.setSession(newSession);
        }
        session = txObject.getSessionHolder().getSession();
        if (this.prepareConnection && isSameConnectionForEntireSession(session)) {
            // We're allowed to change the transaction settings of the JDBC Connection.
            if (logger.isDebugEnabled()) {
                logger.debug("Preparing JDBC Connection of Hibernate Session [" + session + "]");
            }
            Connection con = ((SessionImplementor) session).connection();
            Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
            txObject.setPreviousIsolationLevel(previousIsolationLevel);
            if (this.allowResultAccessAfterCompletion && !txObject.isNewSession()) {
                int currentHoldability = con.getHoldability();
                if (currentHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT) {
                    txObject.setPreviousHoldability(currentHoldability);
                    con.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
                }
            }
        } else {
            // Not allowed to change the transaction settings of the JDBC Connection.
            if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
                // We should set a specific isolation level but are not allowed to...
                throw new InvalidIsolationLevelException("HibernateTransactionManager is not allowed to support custom isolation levels: " + "make sure that its 'prepareConnection' flag is on (the default) and that the " + "Hibernate connection release mode is set to 'on_close' (the default for JDBC).");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Not preparing JDBC Connection of Hibernate Session [" + session + "]");
            }
        }
        if (definition.isReadOnly() && txObject.isNewSession()) {
            // Just set to MANUAL in case of a new Session for this transaction.
            session.setFlushMode(FlushMode.MANUAL);
        }
        if (!definition.isReadOnly() && !txObject.isNewSession()) {
            // We need AUTO or COMMIT for a non-read-only transaction.
            FlushMode flushMode = SessionFactoryUtils.getFlushMode(session);
            if (FlushMode.MANUAL.equals(flushMode)) {
                session.setFlushMode(FlushMode.AUTO);
                txObject.getSessionHolder().setPreviousFlushMode(flushMode);
            }
        }
        Transaction hibTx;
        // Register transaction timeout.
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            // Use Hibernate's own transaction timeout mechanism on Hibernate 3.1+
            // Applies to all statements, also to inserts, updates and deletes!
            hibTx = session.getTransaction();
            hibTx.setTimeout(timeout);
            hibTx.begin();
        } else {
            // Open a plain Hibernate transaction without specified timeout.
            hibTx = session.beginTransaction();
        }
        // Add the Hibernate transaction to the session holder.
        txObject.getSessionHolder().setTransaction(hibTx);
        // Register the Hibernate Session's JDBC Connection for the DataSource, if set.
        if (getDataSource() != null) {
            Connection con = ((SessionImplementor) session).connection();
            ConnectionHolder conHolder = new ConnectionHolder(con);
            if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
                conHolder.setTimeoutInSeconds(timeout);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Exposing Hibernate transaction as JDBC transaction [" + con + "]");
            }
            TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
            txObject.setConnectionHolder(conHolder);
        }
        // Bind the session holder to the thread.
        if (txObject.isNewSessionHolder()) {
            TransactionSynchronizationManager.bindResource(getSessionFactory(), txObject.getSessionHolder());
        }
        txObject.getSessionHolder().setSynchronizedWithTransaction(true);
    } catch (Throwable ex) {
        if (txObject.isNewSession()) {
            try {
                if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
                    session.getTransaction().rollback();
                }
            } catch (Throwable ex2) {
                logger.debug("Could not rollback Session after failed transaction begin", ex);
            } finally {
                SessionFactoryUtils.closeSession(session);
                txObject.setSessionHolder(null);
            }
        }
        throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex);
    }
}
Also used : CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) Connection(java.sql.Connection) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) FlushMode(org.hibernate.FlushMode) ConnectionHolder(org.springframework.jdbc.datasource.ConnectionHolder) Transaction(org.hibernate.Transaction) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Interceptor(org.hibernate.Interceptor) Session(org.hibernate.Session)

Example 3 with InvalidIsolationLevelException

use of org.springframework.transaction.InvalidIsolationLevelException in project spring-framework by spring-projects.

the class JpaTransactionManagerTests method testInvalidIsolation.

@Test
public void testInvalidIsolation() {
    tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
    given(manager.isOpen()).willReturn(true);
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            }
        });
        fail("Should have thrown InvalidIsolationLevelException");
    } catch (InvalidIsolationLevelException ex) {
    // expected
    }
    verify(manager).close();
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) Test(org.junit.Test)

Example 4 with InvalidIsolationLevelException

use of org.springframework.transaction.InvalidIsolationLevelException in project ignite by apache.

the class SpringTransactionManager method doBegin.

/** {@inheritDoc} */
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
    if (definition.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED)
        throw new InvalidIsolationLevelException("Ignite does not support READ_UNCOMMITTED isolation level.");
    IgniteTransactionObject txObj = (IgniteTransactionObject) transaction;
    Transaction tx = null;
    try {
        if (txObj.getTransactionHolder() == null || txObj.getTransactionHolder().isSynchronizedWithTransaction()) {
            long timeout = ignite.configuration().getTransactionConfiguration().getDefaultTxTimeout();
            if (definition.getTimeout() > 0)
                timeout = TimeUnit.SECONDS.toMillis(definition.getTimeout());
            Transaction newTx = ignite.transactions().txStart(transactionConcurrency, convertToIgniteIsolationLevel(definition.getIsolationLevel()), timeout, 0);
            if (log.isDebugEnabled())
                log.debug("Started Ignite transaction: " + newTx);
            txObj.setTransactionHolder(new IgniteTransactionHolder(newTx), true);
        }
        txObj.getTransactionHolder().setSynchronizedWithTransaction(true);
        txObj.getTransactionHolder().setTransactionActive(true);
        tx = txObj.getTransactionHolder().getTransaction();
        // Bind the session holder to the thread.
        if (txObj.isNewTransactionHolder())
            TransactionSynchronizationManager.bindResource(this.ignite, txObj.getTransactionHolder());
    } catch (Exception ex) {
        if (tx != null)
            tx.close();
        throw new CannotCreateTransactionException("Could not create Ignite transaction", ex);
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) IgniteException(org.apache.ignite.IgniteException) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException)

Example 5 with InvalidIsolationLevelException

use of org.springframework.transaction.InvalidIsolationLevelException in project spring-framework by spring-projects.

the class HibernateJpaDialect method beginTransaction.

@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException {
    Session session = getSession(entityManager);
    if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
        session.getTransaction().setTimeout(definition.getTimeout());
    }
    boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT);
    Integer previousIsolationLevel = null;
    Connection preparedCon = null;
    if (isolationLevelNeeded || definition.isReadOnly()) {
        if (this.prepareConnection) {
            preparedCon = HibernateConnectionHandle.doGetConnection(session);
            previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition);
        } else if (isolationLevelNeeded) {
            throw new InvalidIsolationLevelException(getClass().getSimpleName() + " does not support custom isolation levels since the 'prepareConnection' flag is off.");
        }
    }
    // Standard JPA transaction begin call for full JPA context setup...
    entityManager.getTransaction().begin();
    // Adapt flush mode and store previous isolation level, if any.
    FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly());
    return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel);
}
Also used : Connection(java.sql.Connection) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) FlushMode(org.hibernate.FlushMode) Session(org.hibernate.Session)

Aggregations

InvalidIsolationLevelException (org.springframework.transaction.InvalidIsolationLevelException)5 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)3 Connection (java.sql.Connection)2 FlushMode (org.hibernate.FlushMode)2 Session (org.hibernate.Session)2 Connection (javax.jms.Connection)1 Session (javax.jms.Session)1 IgniteException (org.apache.ignite.IgniteException)1 Transaction (org.apache.ignite.transactions.Transaction)1 Interceptor (org.hibernate.Interceptor)1 Transaction (org.hibernate.Transaction)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 Test (org.junit.Test)1 ConnectionHolder (org.springframework.jdbc.datasource.ConnectionHolder)1 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)1 TransactionException (org.springframework.transaction.TransactionException)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionSystemException (org.springframework.transaction.TransactionSystemException)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1