Search in sources :

Example 86 with Synchronization

use of javax.transaction.Synchronization in project datanucleus-core by datanucleus.

the class ResourcedTransaction method rollback.

public void rollback() {
    if (completing) {
        return;
    }
    try {
        completing = true;
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug("Rolling back " + toString());
        }
        // Must be ACTIVE and MARKED ROLLBACK
        if (status != STATUS_ACTIVE && status != STATUS_MARKED_ROLLBACK) {
            throw new IllegalStateException();
        }
        List failures = null;
        status = STATUS_ROLLING_BACK;
        Iterator<Map.Entry<Xid, XAResource>> branchesEntryIter = branches.entrySet().iterator();
        while (branchesEntryIter.hasNext()) {
            Map.Entry<Xid, XAResource> branchesEntry = branchesEntryIter.next();
            Xid xid = branchesEntry.getKey();
            XAResource resourceManager = branchesEntry.getValue();
            try {
                resourceManager.rollback(xid);
            } catch (Throwable e) {
                if (failures == null) {
                    // lazy instantiate this, because we only need on failures
                    failures = new ArrayList();
                }
                failures.add(e);
                NucleusLogger.TRANSACTION.error(Localiser.msg("015038", "rollback", resourceManager, getXAErrorCode(e), toString(), StringUtils.getMessageFromRootCauseOfThrowable(e)));
            }
        }
        status = STATUS_ROLLEDBACK;
        // Synchronization.afterCompletion
        if (synchronization != null) {
            Iterator<Synchronization> syncIterator = synchronization.iterator();
            while (syncIterator.hasNext()) {
                syncIterator.next().afterCompletion(status);
            }
        }
    } finally {
        completing = false;
    }
}
Also used : Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Synchronization(javax.transaction.Synchronization) HashMap(java.util.HashMap) Map(java.util.Map)

Example 87 with Synchronization

use of javax.transaction.Synchronization in project tests by datanucleus.

the class TransactionTest method testUpdateDuringBeforeCompletion.

/**
 * JDO2 $13.4.3 Synchronization: "During the user's beforeCompletion method, fields in persistent and
 * transactional instances might be changed, persistent instances might be deleted, and instances might be
 * made persistent. These changes will be reflected in the current transaction."
 */
public void testUpdateDuringBeforeCompletion() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setNontransactionalRead(true);
        person = null;
        tx.setSynchronization(new Synchronization() {

            public void beforeCompletion() {
                person.setEmailAddress(emailValueBeforeCompletion);
            }

            public void afterCompletion(int arg0) {
            }
        });
        try {
            // test on P_NEW object
            tx.begin();
            emailValueBeforeCompletion = "fred@jpox.org";
            person = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            pm.makePersistent(person);
            tx.commit();
            verifyStringSetDuringBeforeCompletion(pm, person);
            // test on HOLLOW object
            emailValueBeforeCompletion = "fred.flintstone@aol.com";
            tx.begin();
            tx.commit();
            verifyStringSetDuringBeforeCompletion(pm, person);
        } finally {
            tx.setSynchronization(null);
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Synchronization(javax.transaction.Synchronization) Person(org.jpox.samples.models.company.Person)

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