Search in sources :

Example 1 with CannotCreateTransactionException

use of org.springframework.transaction.CannotCreateTransactionException 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 2 with CannotCreateTransactionException

use of org.springframework.transaction.CannotCreateTransactionException in project hazelcast by hazelcast.

the class HazelcastTransactionManager method doBegin.

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
    HazelcastTransactionObject txObject = (HazelcastTransactionObject) transaction;
    try {
        if (txObject.getTransactionContextHolder() == null) {
            TransactionContext transactionContext = hazelcastInstance.newTransactionContext();
            if (logger.isDebugEnabled()) {
                logger.debug("Opened new TransactionContext [" + transactionContext + "]");
            }
            txObject.setTransactionContextHolder(new TransactionContextHolder(transactionContext), true);
        }
        txObject.getTransactionContextHolder().beginTransaction();
        if (txObject.isNewTransactionContextHolder()) {
            TransactionSynchronizationManager.bindResource(hazelcastInstance, txObject.getTransactionContextHolder());
        }
    } catch (Throwable ex) {
        closeTransactionContextAfterFailedBegin(txObject);
        throw new CannotCreateTransactionException("Could not begin Hazelcast transaction", ex);
    }
}
Also used : TransactionContext(com.hazelcast.transaction.TransactionContext) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException)

Example 3 with CannotCreateTransactionException

use of org.springframework.transaction.CannotCreateTransactionException 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");
    }
    ConnectionFactory connectionFactory = obtainConnectionFactory();
    JmsTransactionObject txObject = (JmsTransactionObject) transaction;
    Connection con = null;
    Session session = null;
    try {
        JmsResourceHolder resourceHolder;
        if (this.lazyResourceRetrieval) {
            resourceHolder = new LazyJmsResourceHolder(connectionFactory);
        } else {
            con = createConnection();
            session = createSession(con);
            if (logger.isDebugEnabled()) {
                logger.debug("Created JMS transaction on Session [" + session + "] from Connection [" + con + "]");
            }
            resourceHolder = new JmsResourceHolder(connectionFactory, con, session);
        }
        resourceHolder.setSynchronizedWithTransaction(true);
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            resourceHolder.setTimeoutInSeconds(timeout);
        }
        txObject.setResourceHolder(resourceHolder);
        TransactionSynchronizationManager.bindResource(connectionFactory, resourceHolder);
    } 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 : ConnectionFactory(jakarta.jms.ConnectionFactory) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Connection(jakarta.jms.Connection) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) Session(jakarta.jms.Session)

Example 4 with CannotCreateTransactionException

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

the class HibernateTransactionManager method doBegin.

@Override
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.");
    }
    SessionImplementor session = null;
    try {
        if (!txObject.hasSessionHolder() || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
            Interceptor entityInterceptor = getEntityInterceptor();
            Session newSession = (entityInterceptor != null ? obtainSessionFactory().withOptions().interceptor(entityInterceptor).openSession() : obtainSessionFactory().openSession());
            if (this.sessionInitializer != null) {
                this.sessionInitializer.accept(newSession);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
            }
            txObject.setSession(newSession);
        }
        session = txObject.getSessionHolder().getSession().unwrap(SessionImplementor.class);
        boolean holdabilityNeeded = this.allowResultAccessAfterCompletion && !txObject.isNewSession();
        boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT);
        if (holdabilityNeeded || isolationLevelNeeded || definition.isReadOnly()) {
            if (this.prepareConnection && ConnectionReleaseMode.ON_CLOSE.equals(session.getJdbcCoordinator().getLogicalConnection().getConnectionHandlingMode().getReleaseMode())) {
                // 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 = session.connection();
                Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
                txObject.setPreviousIsolationLevel(previousIsolationLevel);
                txObject.setReadOnly(definition.isReadOnly());
                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);
                    }
                }
                txObject.connectionPrepared();
            } else {
                // Not allowed to change the transaction settings of the JDBC Connection.
                if (isolationLevelNeeded) {
                    // 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.");
                }
                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.setHibernateFlushMode(FlushMode.MANUAL);
            // As of 5.1, we're also setting Hibernate's read-only entity mode by default.
            session.setDefaultReadOnly(true);
        }
        if (!definition.isReadOnly() && !txObject.isNewSession()) {
            // We need AUTO or COMMIT for a non-read-only transaction.
            FlushMode flushMode = session.getHibernateFlushMode();
            if (FlushMode.MANUAL.equals(flushMode)) {
                session.setHibernateFlushMode(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) {
            ConnectionHolder conHolder = new ConnectionHolder(session::connection);
            if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
                conHolder.setTimeoutInSeconds(timeout);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Exposing Hibernate transaction as JDBC [" + conHolder.getConnectionHandle() + "]");
            }
            TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
            txObject.setConnectionHolder(conHolder);
        }
        // Bind the session holder to the thread.
        if (txObject.isNewSessionHolder()) {
            TransactionSynchronizationManager.bindResource(obtainSessionFactory(), txObject.getSessionHolder());
        }
        txObject.getSessionHolder().setSynchronizedWithTransaction(true);
    } catch (Throwable ex) {
        if (txObject.isNewSession()) {
            try {
                if (session != null && 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 5 with CannotCreateTransactionException

use of org.springframework.transaction.CannotCreateTransactionException in project grails-core by grails.

the class ChainedTransactionManager method getTransaction.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition)
	 */
public MultiTransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
    MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
    if (!synchronizationManager.isSynchronizationActive() && canCreateTransaction(definition)) {
        synchronizationManager.initSynchronization();
        mts.setNewSynchronization();
    }
    try {
        for (PlatformTransactionManager transactionManager : transactionManagers) {
            mts.registerTransactionManager(definition, transactionManager);
        }
    } catch (Exception ex) {
        Map<PlatformTransactionManager, TransactionStatus> transactionStatuses = mts.getTransactionStatuses();
        for (PlatformTransactionManager transactionManager : transactionManagers) {
            try {
                if (transactionStatuses.get(transactionManager) != null) {
                    transactionManager.rollback(transactionStatuses.get(transactionManager));
                }
            } catch (Exception ex2) {
                LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex2.getMessage(), ex2);
            }
        }
        if (mts.isNewSynchronization()) {
            synchronizationManager.clearSynchronization();
        }
        throw new CannotCreateTransactionException(ex.getMessage(), ex);
    }
    return mts;
}
Also used : CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Map(java.util.Map) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException)

Aggregations

CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)11 TransactionException (org.springframework.transaction.TransactionException)4 InvalidIsolationLevelException (org.springframework.transaction.InvalidIsolationLevelException)3 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)3 Method (java.lang.reflect.Method)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 Test (org.junit.jupiter.api.Test)2 ConnectionHolder (org.springframework.jdbc.datasource.ConnectionHolder)2 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)2 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)2 TransactionContext (com.hazelcast.transaction.TransactionContext)1 Connection (jakarta.jms.Connection)1 ConnectionFactory (jakarta.jms.ConnectionFactory)1 Session (jakarta.jms.Session)1 EntityManager (javax.persistence.EntityManager)1 NotSupportedException (javax.resource.NotSupportedException)1 Connection (javax.resource.cci.Connection)1