Search in sources :

Example 1 with SystemException

use of jakarta.transaction.SystemException 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 2 with SystemException

use of jakarta.transaction.SystemException 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 3 with SystemException

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnBegin.

@Test
public void jtaTransactionManagerWithSystemExceptionOnBegin() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
    willThrow(new SystemException("system exception")).given(ut).begin();
    assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            }
        });
    });
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) SystemException(jakarta.transaction.SystemException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 4 with SystemException

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException() 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);
    willThrow(new SystemException()).given(ut).begin();
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
        }
    }));
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(tm).resume(tx);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(jakarta.transaction.Transaction) UserTransaction(jakarta.transaction.UserTransaction) SystemException(jakarta.transaction.SystemException) 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 SystemException

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnIsExisting.

@Test
public void jtaTransactionManagerWithSystemExceptionOnIsExisting() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willThrow(new SystemException("system exception"));
    assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            }
        });
    });
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) SystemException(jakarta.transaction.SystemException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Aggregations

SystemException (jakarta.transaction.SystemException)13 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)8 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)8 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 UserTransaction (jakarta.transaction.UserTransaction)7 Test (org.junit.jupiter.api.Test)7 Transaction (jakarta.transaction.Transaction)4 SQLException (java.sql.SQLException)4 RollbackException (jakarta.transaction.RollbackException)3 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)3 HeuristicMixedException (jakarta.transaction.HeuristicMixedException)2 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)2 TransactionManager (jakarta.transaction.TransactionManager)2 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)2 InvalidTransactionException (jakarta.transaction.InvalidTransactionException)1 NotSupportedException (jakarta.transaction.NotSupportedException)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1