Search in sources :

Example 11 with RollbackException

use of javax.transaction.RollbackException in project ignite by apache.

the class TestJtaTxServlet method doGet.

/** {@inheritDoc} */
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
    final int key1 = 1;
    final int key2 = 2;
    final String correctVal1 = "correct_val1";
    final String correctVal2 = "correct_val1";
    final String incorrectVal1 = "incorrect_val2";
    final String incorrectVal2 = "incorrect_val2";
    final PrintWriter writer = res.getWriter();
    try {
        final Ignite ignite = Ignition.ignite();
        final IgniteCache<Integer, String> cache = ignite.cache("tx");
        TransactionManager tmMgr = TransactionManagerFactory.getTransactionManager();
        tmMgr.begin();
        cache.put(key1, correctVal1);
        cache.put(key2, correctVal2);
        writer.println("Transaction #1. Put values [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
        writer.println();
        tmMgr.commit();
        try {
            tmMgr.begin();
            writer.println("Transaction #2. Current values [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
            cache.put(key1, incorrectVal1);
            cache.put(key2, incorrectVal2);
            writer.println("Transaction #2. Put values [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
            tmMgr.setRollbackOnly();
            tmMgr.commit();
        } catch (final RollbackException ignored) {
            writer.println("Transaction #2. setRollbackOnly [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
        }
        writer.println();
        tmMgr.begin();
        writer.println("Transaction #2. Current values [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
        tmMgr.commit();
    } catch (final Throwable e) {
        e.printStackTrace(writer);
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) Ignite(org.apache.ignite.Ignite) RollbackException(javax.transaction.RollbackException) PrintWriter(java.io.PrintWriter)

Example 12 with RollbackException

use of javax.transaction.RollbackException in project jackrabbit by apache.

the class UserTransactionImpl method commit.

/**
     * @see javax.transaction.UserTransaction#commit
     */
public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
    if (status != Status.STATUS_ACTIVE) {
        throw new IllegalStateException("Transaction not active");
    }
    try {
        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
            XAResource resource = (XAResource) it.next();
            XidImpl xid = (XidImpl) xaResources.get(resource);
            resource.end(xid, XAResource.TMSUCCESS);
        }
        status = Status.STATUS_PREPARING;
        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
            XAResource resource = (XAResource) it.next();
            XidImpl xid = (XidImpl) xaResources.get(resource);
            resource.prepare(xid);
        }
        status = Status.STATUS_PREPARED;
        status = Status.STATUS_COMMITTING;
        if (distributedThreadAccess) {
            Thread distributedThread = new Thread() {

                public void run() {
                    try {
                        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
                            XAResource resource = (XAResource) it.next();
                            XidImpl xid = (XidImpl) xaResources.get(resource);
                            resource.commit(xid, false);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                }
            };
            distributedThread.start();
            distributedThread.join(1000);
            if (distributedThread.isAlive()) {
                throw new SystemException("Commit from different thread but same XID must not block");
            }
        } else {
            for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
                XAResource resource = (XAResource) it.next();
                XidImpl xid = (XidImpl) xaResources.get(resource);
                resource.commit(xid, false);
            }
        }
        status = Status.STATUS_COMMITTED;
    } catch (XAException e) {
        if (e.errorCode >= XAException.XA_RBBASE && e.errorCode <= XAException.XA_RBEND) {
            RollbackException re = new RollbackException("Transaction rolled back: XA_ERR=" + e.errorCode);
            re.initCause(e.getCause());
            throw re;
        } else {
            SystemException se = new SystemException("Unable to commit transaction: XA_ERR=" + e.errorCode);
            se.initCause(e.getCause());
            throw se;
        }
    } catch (InterruptedException e) {
        throw new SystemException("Thread.join() interrupted");
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) Iterator(java.util.Iterator) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) XAException(javax.transaction.xa.XAException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 13 with RollbackException

use of javax.transaction.RollbackException in project jackrabbit by apache.

the class XATest method testConflictingCheckin.

/**
     * Checkin from two sessions simultaneously should throw when committing.
     * @throws Exception
     */
public void testConflictingCheckin() throws Exception {
    // get user transaction object
    UserTransaction utx = new UserTransactionImpl(superuser);
    // add node and save
    Node n = testRootNode.addNode(nodeName1, testNodeType);
    n.addMixin(mixVersionable);
    testRootNode.save();
    // reference node in other session
    Node nOther = otherSuperuser.getNodeByUUID(n.getUUID());
    // start transaction
    utx.begin();
    // checkin node inside tx
    n.checkin();
    // checkin node outside tx
    nOther.checkin();
    // commit
    try {
        utx.commit();
        fail("Commit failing with modified version history.");
    } catch (RollbackException e) {
    // expected
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Node(javax.jcr.Node) RollbackException(javax.transaction.RollbackException)

Example 14 with RollbackException

use of javax.transaction.RollbackException in project wildfly by wildfly.

the class WeldTransactionServices method registerSynchronization.

@Override
public void registerSynchronization(Synchronization synchronizedObserver) {
    try {
        final Synchronization synchronization;
        if (!jtsEnabled) {
            synchronization = synchronizedObserver;
        } else {
            synchronization = new JTSSynchronizationWrapper(synchronizedObserver);
        }
        injectedTransactionManager.getValue().getTransaction().registerSynchronization(synchronization);
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (RollbackException e) {
        throw new RuntimeException(e);
    } catch (SystemException e) {
        throw new RuntimeException(e);
    }
}
Also used : SystemException(javax.transaction.SystemException) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException)

Example 15 with RollbackException

use of javax.transaction.RollbackException in project wildfly by wildfly.

the class MessageEndpointInvocationHandler method afterDelivery.

@Override
public void afterDelivery() throws ResourceException {
    final TransactionManager tm = getTransactionManager();
    try {
        if (currentTx != null) {
            if (currentTx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
                tm.rollback();
            else
                tm.commit();
            currentTx = null;
        }
        if (previousTx != null) {
            tm.resume(previousTx);
            previousTx = null;
        }
    } catch (InvalidTransactionException e) {
        throw new LocalTransactionException(e);
    } catch (HeuristicMixedException e) {
        throw new LocalTransactionException(e);
    } catch (SystemException e) {
        throw new LocalTransactionException(e);
    } catch (HeuristicRollbackException e) {
        throw new LocalTransactionException(e);
    } catch (RollbackException e) {
        throw new LocalTransactionException(e);
    } finally {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(previousClassLoader);
        previousClassLoader = null;
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) LocalTransactionException(javax.resource.spi.LocalTransactionException) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) HeuristicMixedException(javax.transaction.HeuristicMixedException) InvalidTransactionException(javax.transaction.InvalidTransactionException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Aggregations

RollbackException (javax.transaction.RollbackException)57 SystemException (javax.transaction.SystemException)25 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)15 Transaction (javax.transaction.Transaction)14 UserTransaction (javax.transaction.UserTransaction)14 HeuristicMixedException (javax.transaction.HeuristicMixedException)12 Test (org.junit.Test)11 Connection (java.sql.Connection)8 XAException (javax.transaction.xa.XAException)8 PreparedStatement (java.sql.PreparedStatement)7 NotSupportedException (javax.transaction.NotSupportedException)7 IOException (java.io.IOException)6 EJBException (javax.ejb.EJBException)6 Account (org.apache.openejb.test.object.Account)6 RemoteException (java.rmi.RemoteException)5 CreateException (javax.ejb.CreateException)5 DataSource (javax.sql.DataSource)5 SQLException (java.sql.SQLException)4 Synchronization (javax.transaction.Synchronization)4 XAResource (javax.transaction.xa.XAResource)4