Search in sources :

Example 1 with Synchronization

use of javax.transaction.Synchronization in project hibernate-orm by hibernate.

the class XaTransactionImpl method commit.

public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, IllegalStateException, SystemException {
    if (status == Status.STATUS_MARKED_ROLLBACK) {
        log.trace("on commit, status was marked for rollback-only");
        rollback();
    } else {
        status = Status.STATUS_PREPARING;
        if (synchronizations != null) {
            for (int i = 0; i < synchronizations.size(); i++) {
                Synchronization s = (Synchronization) synchronizations.get(i);
                s.beforeCompletion();
            }
        }
        if (!runXaResourcePrepare()) {
            status = Status.STATUS_ROLLING_BACK;
        } else {
            status = Status.STATUS_PREPARED;
        }
        status = Status.STATUS_COMMITTING;
        if (connection != null) {
            try {
                connection.commit();
                connectionProvider.closeConnection(connection);
                connection = null;
            } catch (SQLException sqle) {
                status = Status.STATUS_UNKNOWN;
                throw new SystemException();
            }
        }
        runXaResourceCommitTx();
        status = Status.STATUS_COMMITTED;
        if (synchronizations != null) {
            for (int i = 0; i < synchronizations.size(); i++) {
                Synchronization s = (Synchronization) synchronizations.get(i);
                s.afterCompletion(status);
            }
        }
        // status = Status.STATUS_NO_TRANSACTION;
        jtaTransactionManager.endCurrent(this);
    }
}
Also used : SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) Synchronization(javax.transaction.Synchronization)

Example 2 with Synchronization

use of javax.transaction.Synchronization in project hibernate-orm by hibernate.

the class DualNodeJtaTransactionImpl method commit.

public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, IllegalStateException, SystemException {
    if (status == Status.STATUS_MARKED_ROLLBACK) {
        log.trace("on commit, status was marked for rollback-only");
        rollback();
    } else {
        status = Status.STATUS_PREPARING;
        if (synchronizations != null) {
            for (int i = 0; i < synchronizations.size(); i++) {
                Synchronization s = (Synchronization) synchronizations.get(i);
                s.beforeCompletion();
            }
        }
        if (!runXaResourcePrepare()) {
            status = Status.STATUS_ROLLING_BACK;
        } else {
            status = Status.STATUS_PREPARED;
        }
        status = Status.STATUS_COMMITTING;
        if (connection != null) {
            try {
                connection.commit();
                connection.close();
            } catch (SQLException sqle) {
                status = Status.STATUS_UNKNOWN;
                throw new SystemException();
            }
        }
        runXaResourceCommitTx();
        status = Status.STATUS_COMMITTED;
        if (synchronizations != null) {
            for (int i = 0; i < synchronizations.size(); i++) {
                Synchronization s = (Synchronization) synchronizations.get(i);
                s.afterCompletion(status);
            }
        }
        // status = Status.STATUS_NO_TRANSACTION;
        jtaTransactionManager.endCurrent(this);
    }
}
Also used : SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) Synchronization(javax.transaction.Synchronization)

Example 3 with Synchronization

use of javax.transaction.Synchronization in project hibernate-orm by hibernate.

the class DualNodeJtaTransactionImpl method rollback.

public void rollback() throws IllegalStateException, SystemException {
    status = Status.STATUS_ROLLING_BACK;
    runXaResourceRollback();
    status = Status.STATUS_ROLLEDBACK;
    if (connection != null) {
        try {
            connection.rollback();
            connection.close();
        } catch (SQLException sqle) {
            status = Status.STATUS_UNKNOWN;
            throw new SystemException();
        }
    }
    if (synchronizations != null) {
        for (int i = 0; i < synchronizations.size(); i++) {
            Synchronization s = (Synchronization) synchronizations.get(i);
            s.afterCompletion(status);
        }
    }
    // status = Status.STATUS_NO_TRANSACTION;
    jtaTransactionManager.endCurrent(this);
}
Also used : SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) Synchronization(javax.transaction.Synchronization)

Example 4 with Synchronization

use of javax.transaction.Synchronization in project wildfly by wildfly.

the class InfinispanBatcherTestCase method createBatchDiscard.

@Test
public void createBatchDiscard() throws Exception {
    Transaction tx = mock(Transaction.class);
    ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
    when(this.tm.getTransaction()).thenReturn(tx);
    try (TransactionBatch batch = this.batcher.createBatch()) {
        verify(this.tm).begin();
        verify(tx).registerSynchronization(capturedSync.capture());
        assertSame(tx, batch.getTransaction());
        batch.discard();
    } finally {
        capturedSync.getValue().afterCompletion(Status.STATUS_ROLLEDBACK);
    }
    verify(tx, never()).commit();
    verify(tx).rollback();
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Example 5 with Synchronization

use of javax.transaction.Synchronization in project wildfly by wildfly.

the class InfinispanBatcherTestCase method createNestedBatchDiscard.

@Test
public void createNestedBatchDiscard() throws Exception {
    Transaction tx = mock(Transaction.class);
    ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
    when(this.tm.getTransaction()).thenReturn(tx);
    try (TransactionBatch outerBatch = this.batcher.createBatch()) {
        verify(this.tm).begin();
        verify(tx).registerSynchronization(capturedSync.capture());
        reset(this.tm);
        assertSame(tx, outerBatch.getTransaction());
        when(tx.getStatus()).thenReturn(Status.STATUS_ACTIVE);
        when(this.tm.getTransaction()).thenReturn(tx);
        try (TransactionBatch innerBatch = this.batcher.createBatch()) {
            verify(this.tm, never()).begin();
            innerBatch.discard();
        }
        verify(tx, never()).commit();
        verify(tx, never()).rollback();
    } finally {
        capturedSync.getValue().afterCompletion(Status.STATUS_ROLLEDBACK);
    }
    verify(tx).rollback();
    verify(tx, never()).commit();
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Aggregations

Synchronization (javax.transaction.Synchronization)87 Test (org.junit.Test)42 Transaction (javax.transaction.Transaction)23 SystemException (javax.transaction.SystemException)21 RollbackException (javax.transaction.RollbackException)14 SQLException (java.sql.SQLException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)7 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 UserTransaction (javax.transaction.UserTransaction)5 XAResource (javax.transaction.xa.XAResource)5 EJBException (javax.ejb.EJBException)4 HeuristicMixedException (javax.transaction.HeuristicMixedException)4 TransactionImple (com.arjuna.ats.internal.jta.transaction.jts.TransactionImple)3 List (java.util.List)3 Xid (javax.transaction.xa.Xid)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3