Search in sources :

Example 46 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 47 with Synchronization

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

the class EJBTimerService method cancelTimerSynchronization.

private 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)

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