Search in sources :

Example 41 with Synchronization

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

the class InfinispanBatcherTestCase method createBatchClose.

@Test
public void createBatchClose() 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());
    } finally {
        capturedSync.getValue().afterCompletion(Status.STATUS_COMMITTED);
    }
    verify(tx).commit();
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Example 42 with Synchronization

use of javax.transaction.Synchronization in project geode by apache.

the class GlobalTransactionJUnitTest method testRegisterSynchronization.

@Test
public void testRegisterSynchronization() throws Exception {
    try {
        boolean exceptionoccurred = false;
        utx.begin();
        try {
            Transaction txn = tm.getTransaction();
            Synchronization sync = new SyncImpl();
            txn.registerSynchronization(sync);
        } catch (RollbackException e) {
            exceptionoccurred = true;
        }
        if (exceptionoccurred)
            fail("exception occurred while trying to register synchronization ");
        utx.rollback();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        fail("exception in testRegisterSynchronization due to " + e);
        e.printStackTrace();
    }
}
Also used : Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 43 with Synchronization

use of javax.transaction.Synchronization in project geode by apache.

the class TransactionImplJUnitTest method testRegisterSynchronization.

@Test
public void testRegisterSynchronization() throws Exception {
    utx.begin();
    TransactionImpl txn = (TransactionImpl) tm.getTransaction();
    Synchronization sync = new SyncImpl();
    txn.registerSynchronization(sync);
    assertTrue("Synchronization not registered succesfully", txn.getSyncList().contains(sync));
    utx.commit();
}
Also used : Synchronization(javax.transaction.Synchronization) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 44 with Synchronization

use of javax.transaction.Synchronization in project geode by apache.

the class GlobalTransactionJUnitTest method testRegisterSynchronizationAfterRollBack.

@Test
public void testRegisterSynchronizationAfterRollBack() throws Exception {
    try {
        boolean exceptionoccurred = false;
        utx.begin();
        utx.setRollbackOnly();
        Context ctx = cache.getJNDIContext();
        try {
            Transaction txn = tm.getTransaction();
            Synchronization sync = new SyncImpl();
            txn.registerSynchronization(sync);
        } catch (RollbackException e) {
            exceptionoccurred = true;
        }
        if (!exceptionoccurred)
            fail("RollbackException not occurred although the transaction was marked for rollback");
        utx.rollback();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        fail("exception in testSetRollbackonly due to " + e);
        e.printStackTrace();
    }
}
Also used : Context(javax.naming.Context) Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 45 with Synchronization

use of javax.transaction.Synchronization in project Payara by payara.

the class SynchronizationImpl method before_completion.

public void before_completion() {
    // Regular syncs first then the interposed syncs
    Enumeration e = syncs.elements();
    while (e.hasMoreElements()) {
        Synchronization sync = (Synchronization) e.nextElement();
        try {
            sync.beforeCompletion();
        } catch (RuntimeException rex) {
            try {
                state.setRollbackOnly();
            } catch (Exception ex1) {
                _logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", ex1);
            }
            _logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", rex);
            throw rex;
        } catch (Exception ex) {
            _logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", ex);
        }
    }
    Enumeration e1 = interposedSyncs.elements();
    while (e1.hasMoreElements()) {
        Synchronization sync = (Synchronization) e1.nextElement();
        try {
            sync.beforeCompletion();
        } catch (RuntimeException rex) {
            try {
                state.setRollbackOnly();
            } catch (Exception ex1) {
                _logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", ex1);
            }
            _logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", rex);
            throw rex;
        } catch (Exception ex) {
            _logger.log(Level.WARNING, "jts.unexpected_error_occurred_in_after_completion", ex);
        }
    }
    state.beforeCompletion();
}
Also used : Synchronization(javax.transaction.Synchronization)

Aggregations

Synchronization (javax.transaction.Synchronization)47 Test (org.junit.Test)20 Transaction (javax.transaction.Transaction)10 SystemException (javax.transaction.SystemException)9 SQLException (java.sql.SQLException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 RollbackException (javax.transaction.RollbackException)5 EJBException (javax.ejb.EJBException)4 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)4 TransactionManager (javax.transaction.TransactionManager)3 UserTransaction (javax.transaction.UserTransaction)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 Map (java.util.Map)2 Set (java.util.Set)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ThreadContext (org.apache.openejb.core.ThreadContext)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoSuchObjectException (java.rmi.NoSuchObjectException)1 RemoteException (java.rmi.RemoteException)1 Connection (java.sql.Connection)1