Search in sources :

Example 31 with Synchronization

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

the class TransactionalTest method rollbackException.

@Test
public void rollbackException() throws Exception {
    for (int i = 0; i < 2; i++) {
        final AtomicInteger status = new AtomicInteger();
        final TransactionManager transactionManager = OpenEJB.getTransactionManager();
        transactionManager.begin();
        transactionManager.getTransaction().registerSynchronization(new Synchronization() {

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

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

Example 32 with Synchronization

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

the class TransactionalTest method classLevel.

@Test
public void classLevel() throws Exception {
    for (int i = 0; i < 2; i++) {
        final AtomicInteger status = new AtomicInteger();
        try {
            bean.classLevel(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 AnCheckedException 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 33 with Synchronization

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

the class TransactionalTest method checked.

@Test
public void checked() throws Exception {
    for (int i = 0; i < 2; i++) {
        final AtomicInteger status = new AtomicInteger();
        try {
            bean.checked(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 AnCheckedException 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 34 with Synchronization

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

the class XaTransactionImpl 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);
            if (s != null)
                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 35 with Synchronization

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

the class VersionedTest method testStaleRead.

protected ByRef<Object> testStaleRead(BiConsumer<Session, Item> consumer) throws Exception {
    AtomicReference<Exception> synchronizationException = new AtomicReference<>();
    CountDownLatch syncLatch = new CountDownLatch(1);
    CountDownLatch commitLatch = new CountDownLatch(1);
    Future<Boolean> action = executor.submit(() -> withTxSessionApply(s -> {
        try {
            ((SharedSessionContractImplementor) s).getTransactionCoordinator().getLocalSynchronizations().registerSynchronization(new Synchronization() {

                @Override
                public void beforeCompletion() {
                }

                @Override
                public void afterCompletion(int i) {
                    syncLatch.countDown();
                    try {
                        awaitOrThrow(commitLatch);
                    } catch (Exception e) {
                        synchronizationException.set(e);
                    }
                }
            });
            Item item = s.load(Item.class, itemId);
            consumer.accept(s, item);
            s.flush();
        } catch (StaleStateException e) {
            log.info("Exception thrown: ", e);
            markRollbackOnly(s);
            return false;
        } catch (PessimisticLockException e) {
            log.info("Exception thrown: ", e);
            markRollbackOnly(s);
            return false;
        }
        return true;
    }));
    awaitOrThrow(syncLatch);
    ByRef<Object> entryRef = new ByRef<>(null);
    try {
        withTxSession(s -> {
            Item item = s.load(Item.class, itemId);
            assertEquals("Original item", item.getDescription());
            entryRef.set(assertSingleCacheEntry());
        });
    } finally {
        commitLatch.countDown();
    }
    assertTrue(action.get(WAIT_TIMEOUT, TimeUnit.SECONDS));
    assertNull(synchronizationException.get());
    return entryRef;
}
Also used : BaseTransactionalDataRegion(org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion) Arrays(java.util.Arrays) VersionedEntry(org.hibernate.cache.infinispan.util.VersionedEntry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Session(org.hibernate.Session) Caches(org.hibernate.cache.infinispan.util.Caches) AtomicReference(java.util.concurrent.atomic.AtomicReference) Future(java.util.concurrent.Future) PessimisticLockException(org.hibernate.PessimisticLockException) InvocationContext(org.infinispan.context.InvocationContext) AdvancedCache(org.infinispan.AdvancedCache) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) Synchronization(javax.transaction.Synchronization) StaleStateException(org.hibernate.StaleStateException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ByRef(org.infinispan.commons.util.ByRef) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) BaseCustomInterceptor(org.infinispan.interceptors.base.BaseCustomInterceptor) Assert.assertNull(org.junit.Assert.assertNull) Flag(org.infinispan.context.Flag) Assert.assertFalse(org.junit.Assert.assertFalse) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) ByRef(org.infinispan.commons.util.ByRef) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Synchronization(javax.transaction.Synchronization) PessimisticLockException(org.hibernate.PessimisticLockException) StaleStateException(org.hibernate.StaleStateException) PessimisticLockException(org.hibernate.PessimisticLockException) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) StaleStateException(org.hibernate.StaleStateException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

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