Search in sources :

Example 16 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 17 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)

Example 18 with Synchronization

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

the class InfinispanBatcherTestCase method createOverlappingBatchDiscard.

@SuppressWarnings("resource")
@Test
public void createOverlappingBatchDiscard() throws Exception {
    Transaction tx = mock(Transaction.class);
    ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
    when(this.tm.getTransaction()).thenReturn(tx);
    TransactionBatch batch = this.batcher.createBatch();
    verify(this.tm).begin();
    verify(tx).registerSynchronization(capturedSync.capture());
    reset(this.tm);
    try {
        assertSame(tx, batch.getTransaction());
        when(this.tm.getTransaction()).thenReturn(tx);
        when(tx.getStatus()).thenReturn(Status.STATUS_ACTIVE);
        try (TransactionBatch innerBatch = this.batcher.createBatch()) {
            verify(this.tm, never()).begin();
            innerBatch.discard();
            batch.close();
            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)

Example 19 with Synchronization

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

the class TransactionalTest method runtimeChecked.

@Test
public void runtimeChecked() throws Exception {
    for (int i = 0; i < 2; i++) {
        final AtomicInteger status = new AtomicInteger();
        try {
            bean.runtimeChecked(new Runnable() {

                @Override
                public void run() {
                    try {
                        OpenEJB.getTransactionManager().getTransaction().registerSynchronization(new Synchronization() {

                            @Override
                            public void beforeCompletion() {
                            // no-op
                            }

                            @Override
                            public void afterCompletion(int state) {
                                status.set(state);
                            }
                        });
                    } catch (final RollbackException | SystemException e) {
                        fail();
                    }
                }
            });
            fail();
        } catch (final AnException e) {
        // no-op
        }
        assertEquals(Status.STATUS_COMMITTED, status.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Example 20 with Synchronization

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

the class TransactionalTest method runtimeException.

@Test
public void runtimeException() throws Exception {
    for (int i = 0; i < 2; i++) {
        final AtomicInteger status = new AtomicInteger(-1);
        try {
            bean.runtimeEx(new Runnable() {

                @Override
                public void run() {
                    try {
                        OpenEJB.getTransactionManager().getTransaction().registerSynchronization(new Synchronization() {

                            @Override
                            public void beforeCompletion() {
                            // no-op
                            }

                            @Override
                            public void afterCompletion(int state) {
                                status.set(state);
                            }
                        });
                    } catch (final RollbackException | SystemException e) {
                        fail();
                    }
                }
            });
            fail();
        } catch (final AnException e) {
        // no-op
        }
        assertEquals(Status.STATUS_ROLLEDBACK, status.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Aggregations

Synchronization (javax.transaction.Synchronization)38 Test (org.junit.Test)20 Transaction (javax.transaction.Transaction)10 SQLException (java.sql.SQLException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 SystemException (javax.transaction.SystemException)7 RollbackException (javax.transaction.RollbackException)5 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 TransactionManager (javax.transaction.TransactionManager)3 UserTransaction (javax.transaction.UserTransaction)3 Map (java.util.Map)2 Set (java.util.Set)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 EJBException (javax.ejb.EJBException)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