Search in sources :

Example 1 with Transaction

use of jakarta.transaction.Transaction in project tomcat by apache.

the class TransactionContext method addTransactionContextListener.

/**
 * Adds a listener for transaction completion events.
 *
 * @param listener
 *            the listener to add
 * @throws SQLException
 *             if a problem occurs adding the listener to the transaction
 */
public void addTransactionContextListener(final TransactionContextListener listener) throws SQLException {
    try {
        if (!isActive()) {
            final Transaction transaction = this.transactionRef.get();
            listener.afterCompletion(TransactionContext.this, transaction != null && transaction.getStatus() == Status.STATUS_COMMITTED);
            return;
        }
        final Synchronization s = new Synchronization() {

            @Override
            public void afterCompletion(final int status) {
                listener.afterCompletion(TransactionContext.this, status == Status.STATUS_COMMITTED);
            }

            @Override
            public void beforeCompletion() {
            // empty
            }
        };
        if (transactionSynchronizationRegistry != null) {
            transactionSynchronizationRegistry.registerInterposedSynchronization(s);
        } else {
            getTransaction().registerSynchronization(s);
        }
    } catch (final RollbackException e) {
    // JTA spec doesn't let us register with a transaction marked rollback only
    // just ignore this and the tx state will be cleared another way.
    } catch (final Exception e) {
        throw new SQLException("Unable to register transaction context listener", e);
    }
}
Also used : Transaction(jakarta.transaction.Transaction) SQLException(java.sql.SQLException) Synchronization(jakarta.transaction.Synchronization) RollbackException(jakarta.transaction.RollbackException) SQLException(java.sql.SQLException) RollbackException(jakarta.transaction.RollbackException) SystemException(jakarta.transaction.SystemException)

Example 2 with Transaction

use of jakarta.transaction.Transaction in project tomcat by apache.

the class TransactionContext method isActive.

/**
 * True if the transaction is active or marked for rollback only.
 *
 * @return true if the transaction is active or marked for rollback only; false otherwise
 * @throws SQLException
 *             if a problem occurs obtaining the transaction status
 */
public boolean isActive() throws SQLException {
    try {
        final Transaction transaction = this.transactionRef.get();
        if (transaction == null) {
            return false;
        }
        final int status = transaction.getStatus();
        return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
    } catch (final SystemException e) {
        throw new SQLException("Unable to get transaction status", e);
    }
}
Also used : Transaction(jakarta.transaction.Transaction) SystemException(jakarta.transaction.SystemException) SQLException(java.sql.SQLException)

Example 3 with Transaction

use of jakarta.transaction.Transaction in project tomcat by apache.

the class TransactionRegistry method getActiveTransactionContext.

/**
 * Gets the active TransactionContext or null if not Transaction is active.
 *
 * @return The active TransactionContext or null if no Transaction is active.
 * @throws SQLException
 *             Thrown when an error occurs while fetching the transaction.
 */
public TransactionContext getActiveTransactionContext() throws SQLException {
    Transaction transaction = null;
    try {
        transaction = transactionManager.getTransaction();
        // was there a transaction?
        if (transaction == null) {
            return null;
        }
    // This is the transaction on the thread so no need to check it's status - we should try to use it and
    // fail later based on the subsequent status
    } catch (final SystemException e) {
        throw new SQLException("Unable to determine current transaction ", e);
    }
    // register the context (or create a new one)
    synchronized (this) {
        TransactionContext cache = caches.get(transaction);
        if (cache == null) {
            cache = new TransactionContext(this, transaction, transactionSynchronizationRegistry);
            caches.put(transaction, cache);
        }
        return cache;
    }
}
Also used : Transaction(jakarta.transaction.Transaction) SystemException(jakarta.transaction.SystemException) SQLException(java.sql.SQLException)

Example 4 with Transaction

use of jakarta.transaction.Transaction in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNew.

@Test
public void jtaTransactionManagerWithPropagationRequiresNew() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    final JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    tt.setName("txName");
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName");
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            TransactionTemplate tt2 = new TransactionTemplate(ptm);
            tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            tt2.setReadOnly(true);
            tt2.setName("txName2");
            tt2.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
                    assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName2");
                    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isTrue();
                }
            });
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName");
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(ut, times(2)).begin();
    verify(ut, times(2)).commit();
    verify(tm).resume(tx);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(jakarta.transaction.Transaction) UserTransaction(jakarta.transaction.UserTransaction) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 5 with Transaction

use of jakarta.transaction.Transaction in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExisting.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(ut).begin();
    verify(ut).commit();
    verify(tm).resume(tx);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(jakarta.transaction.Transaction) UserTransaction(jakarta.transaction.UserTransaction) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Aggregations

Transaction (jakarta.transaction.Transaction)9 SystemException (jakarta.transaction.SystemException)5 TransactionManager (jakarta.transaction.TransactionManager)5 UserTransaction (jakarta.transaction.UserTransaction)5 Test (org.junit.jupiter.api.Test)5 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)5 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)5 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)5 SQLException (java.sql.SQLException)4 RollbackException (jakarta.transaction.RollbackException)2 Synchronization (jakarta.transaction.Synchronization)1 XAResource (javax.transaction.xa.XAResource)1