Search in sources :

Example 36 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 37 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 38 with Synchronization

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

the class CmpContainer method ejbLoad.

private void ejbLoad(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.LOAD);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.ejbLoad();
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
    // if we call load we must call store
    try {
        //noinspection unchecked
        Set<EntityBean> registeredEntities = (LinkedHashSet<EntityBean>) synchronizationRegistry.getResource(ENTITIES_TO_STORE);
        if (registeredEntities == null) {
            registeredEntities = new LinkedHashSet<EntityBean>();
            synchronizationRegistry.putResource(ENTITIES_TO_STORE, registeredEntities);
            synchronizationRegistry.registerInterposedSynchronization(new Synchronization() {

                @Override
                public void beforeCompletion() {
                    //noinspection unchecked
                    final Set<EntityBean> registeredEntities = (LinkedHashSet<EntityBean>) synchronizationRegistry.getResource(ENTITIES_TO_STORE);
                    if (registeredEntities == null) {
                        return;
                    }
                    for (final EntityBean entityBean : registeredEntities) {
                        ejbStore(entityBean);
                    }
                }

                @Override
                public void afterCompletion(final int i) {
                }
            });
        }
        registeredEntities.add(entityBean);
    } catch (final Exception e) {
    // no-op
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ThreadContext(org.apache.openejb.core.ThreadContext) Synchronization(javax.transaction.Synchronization) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) NoSuchObjectException(java.rmi.NoSuchObjectException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) FinderException(javax.ejb.FinderException) EntityBean(javax.ejb.EntityBean) RemoteException(java.rmi.RemoteException) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

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