Search in sources :

Example 6 with Transaction

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationNotSupported.

@Test
public void jtaTransactionManagerWithPropagationNotSupported() 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_NOT_SUPPORTED);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            status.setRollbackOnly();
        }
    });
    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) 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 7 with Transaction

use of jakarta.transaction.Transaction 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 8 with Transaction

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

the class TransactionContext method setSharedConnection.

/**
 * Sets the shared connection for this transaction. The shared connection is enlisted in the transaction.
 *
 * @param sharedConnection
 *            the shared connection
 * @throws SQLException
 *             if a shared connection is already set, if XAResource for the connection could not be found in the
 *             transaction registry, or if there was a problem enlisting the connection in the transaction
 */
public void setSharedConnection(final Connection sharedConnection) throws SQLException {
    if (this.sharedConnection != null) {
        throw new IllegalStateException("A shared connection is already set");
    }
    // This is the first use of the connection in this transaction, so we must
    // enlist it in the transaction
    final Transaction transaction = getTransaction();
    try {
        final XAResource xaResource = transactionRegistry.getXAResource(sharedConnection);
        if (!transaction.enlistResource(xaResource)) {
            throw new SQLException("Unable to enlist connection in transaction: enlistResource returns 'false'.");
        }
    } catch (final IllegalStateException e) {
        // This can happen if the transaction is already timed out
        throw new SQLException("Unable to enlist connection in the transaction", e);
    } catch (final RollbackException e) {
    // transaction was rolled back... proceed as if there never was a transaction
    } catch (final SystemException e) {
        throw new SQLException("Unable to enlist connection the transaction", e);
    }
    this.sharedConnection = sharedConnection;
}
Also used : XAResource(javax.transaction.xa.XAResource) Transaction(jakarta.transaction.Transaction) SystemException(jakarta.transaction.SystemException) SQLException(java.sql.SQLException) RollbackException(jakarta.transaction.RollbackException)

Example 9 with Transaction

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndAdapter.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception {
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    JtaTransactionManager ptm = newJtaTransactionManager(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(tm).begin();
    verify(tm).commit();
    verify(tm).resume(tx);
}
Also used : 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