Search in sources :

Example 71 with Synchronization

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

the class EJBTimerService method cancelTimerSynchronization.

protected void cancelTimerSynchronization(EJBContextImpl context_, TimerPrimaryKey timerId, long containerId, String ownerId, boolean persistent) throws Exception {
    // is owned by the current server instance.
    if (context_ == null || timerOwnedByThisServer(ownerId)) {
        // Cancel existing timer task.  Save time at which task would
        // have executed in case cancellation is rolled back.  The
        // nextTimeout can be null if the timer is currently being
        // delivered.
        Date nextTimeout = cancelTask(timerId);
        ContainerSynchronization containerSynch = getContainerSynch(context_, timerId.getTimerId(), persistent);
        if (containerSynch == null) {
            // null ContainerSynchronization for persistent timer would've
            // caused exception in #getContainerSynch(). For non-persistent
            // timer remove it right away
            expungeTimer(timerId);
            ejbContainerUtil.getContainer(containerId).incrementRemovedTimedObject();
        } else {
            Synchronization timerSynch = containerSynch.getTimerSynchronization(timerId);
            if (timerSynch != null) {
                // This timer was created and cancelled within the
                // same transaction.  No tx synchronization actions
                // are needed, since whether tx commits or rolls back,
                // timer will not exist.
                containerSynch.removeTimerSynchronization(timerId);
                expungeTimer(timerId);
            } else {
                // Set tx synchronization action to handle timer cancellation.
                timerSynch = new TimerSynch(timerId, STATE_CANCELLED, nextTimeout, ejbContainerUtil.getContainer(containerId), this);
                containerSynch.addTimerSynchronization(timerId, timerSynch);
            }
        }
    }
}
Also used : Synchronization(javax.transaction.Synchronization) Date(java.util.Date)

Example 72 with Synchronization

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

the class ContainerSynchronization method beforeCompletion.

public void beforeCompletion() {
    // first call beforeCompletion for each bean instance
    for (int i = 0; i < beans.size(); i++) {
        EJBContextImpl context = (EJBContextImpl) beans.get(i);
        BaseContainer container = (BaseContainer) context.getContainer();
        try {
            if (container != null) {
                boolean allowTxCompletion = true;
                if (container.isUndeployed()) {
                    if (context instanceof SessionContextImpl) {
                        allowTxCompletion = ((SessionContextImpl) context).getInLifeCycleCallback();
                    } else {
                        allowTxCompletion = false;
                        _logger.log(Level.WARNING, "Marking Tx for rollback " + " because container for " + container + " is undeployed");
                    }
                }
                if (!allowTxCompletion) {
                    try {
                        tx.setRollbackOnly();
                    } catch (SystemException sysEx) {
                        _logger.log(Level.FINE, "Error while trying to " + "mark for rollback", sysEx);
                    }
                } else {
                    container.beforeCompletion(context);
                }
            } else {
                // Might be null if bean was removed.  Just skip it.
                _logger.log(Level.FINE, "context with empty container in " + " ContainerSynchronization.beforeCompletion");
            }
        } catch (RuntimeException ex) {
            logAndRollbackTransaction(ex);
            throw ex;
        } catch (Exception ex) {
            logAndRollbackTransaction(ex);
            // no need to call remaining beforeCompletions
            throw new EJBException("Error during beforeCompletion.", ex);
        }
    }
    // now call beforeCompletion for all pmSyncs
    for (int i = 0; i < pmSyncs.size(); i++) {
        Synchronization sync = (Synchronization) pmSyncs.elementAt(i);
        try {
            sync.beforeCompletion();
        } catch (RuntimeException ex) {
            logAndRollbackTransaction(ex);
            throw ex;
        } catch (Exception ex) {
            logAndRollbackTransaction(ex);
            // no need to call remaining beforeCompletions
            throw new EJBException("Error during beforeCompletion.", ex);
        }
    }
}
Also used : SystemException(javax.transaction.SystemException) EJBException(javax.ejb.EJBException) Synchronization(javax.transaction.Synchronization) EJBException(javax.ejb.EJBException) SystemException(javax.transaction.SystemException)

Example 73 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<>();
            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)

Example 74 with Synchronization

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

the class BaseEjbProxyHandler method getLiveHandleRegistry.

public ConcurrentMap getLiveHandleRegistry() {
    final BeanContext beanContext = getBeanContext();
    final ThreadContext tc = ThreadContext.getThreadContext();
    if (tc != null && tc.getBeanContext() != beanContext && /* parent bean */
    tc.getCurrentOperation() == Operation.BUSINESS) {
        ProxyRegistry registry = tc.get(ProxyRegistry.class);
        if (registry == null) {
            registry = new ProxyRegistry();
            tc.set(ProxyRegistry.class, registry);
        }
        return registry.liveHandleRegistry;
    } else {
        // use the tx if there
        final SystemInstance systemInstance = SystemInstance.get();
        final TransactionManager txMgr = systemInstance.getComponent(TransactionManager.class);
        try {
            final Transaction tx = txMgr.getTransaction();
            if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE) {
                final TransactionSynchronizationRegistry registry = systemInstance.getComponent(TransactionSynchronizationRegistry.class);
                final String resourceKey = ProxyRegistry.class.getName();
                ConcurrentMap map = ConcurrentMap.class.cast(registry.getResource(resourceKey));
                if (map == null) {
                    map = new ConcurrentHashMap();
                    registry.putResource(resourceKey, map);
                    try {
                        final ConcurrentMap tmp = map;
                        tx.registerSynchronization(new Synchronization() {

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

                            @Override
                            public void afterCompletion(final int status) {
                                tmp.clear();
                            }
                        });
                    } catch (final RollbackException e) {
                    // not really possible since we check the status
                    // let it go to default
                    }
                }
                return map;
            }
        } catch (final SystemException e) {
        // let it go to default
        }
        // back to default but it doesnt release the memory
        ProxyRegistry proxyRegistry = beanContext.get(ProxyRegistry.class);
        if (proxyRegistry == null) {
            proxyRegistry = new ProxyRegistry();
            beanContext.set(ProxyRegistry.class, proxyRegistry);
        }
        return proxyRegistry.liveHandleRegistry;
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) ConcurrentMap(java.util.concurrent.ConcurrentMap) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) BeanContext(org.apache.openejb.BeanContext) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) SystemInstance(org.apache.openejb.loader.SystemInstance) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 75 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)

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