Search in sources :

Example 31 with Transaction

use of javax.transaction.Transaction in project hazelcast by hazelcast.

the class ClientXATest method testWhenLockedOutOfTransaction.

@Test
public void testWhenLockedOutOfTransaction() throws Exception {
    Hazelcast.newHazelcastInstance();
    HazelcastInstance client = HazelcastClient.newHazelcastClient();
    IMap<Object, Object> map = client.getMap("map");
    map.put("key", "value");
    HazelcastXAResource xaResource = client.getXAResource();
    tm.begin();
    Transaction transaction = tm.getTransaction();
    transaction.enlistResource(xaResource);
    TransactionContext context = xaResource.getTransactionContext();
    TransactionalMap<Object, Object> transactionalMap = context.getMap("map");
    if (map.tryLock("key")) {
        transactionalMap.remove("key");
    }
    tm.commit();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Transaction(javax.transaction.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 32 with Transaction

use of javax.transaction.Transaction in project hazelcast by hazelcast.

the class HazelcastXATest method close.

private void close(boolean error, XAResource... xaResource) throws Exception {
    int flag = XAResource.TMSUCCESS;
    // get the current tx
    Transaction tx = tm.getTransaction();
    // closeConnection
    if (error)
        flag = XAResource.TMFAIL;
    for (XAResource resource : xaResource) {
        tx.delistResource(resource, flag);
    }
    if (error)
        tm.rollback();
    else
        tm.commit();
}
Also used : XAResource(javax.transaction.xa.XAResource) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource) Transaction(javax.transaction.Transaction)

Example 33 with Transaction

use of javax.transaction.Transaction in project hibernate-orm by hibernate.

the class JTASessionContext method currentSession.

@Override
public Session currentSession() throws HibernateException {
    final JtaPlatform jtaPlatform = factory().getServiceRegistry().getService(JtaPlatform.class);
    final TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
    if (transactionManager == null) {
        throw new HibernateException("No TransactionManagerLookup specified");
    }
    Transaction txn;
    try {
        txn = transactionManager.getTransaction();
        if (txn == null) {
            throw new HibernateException("Unable to locate current JTA transaction");
        }
        if (!JtaStatusHelper.isActive(txn.getStatus())) {
            // entries cleaned up (aside from spawning threads).
            throw new HibernateException("Current transaction is not in progress");
        }
    } catch (HibernateException e) {
        throw e;
    } catch (Throwable t) {
        throw new HibernateException("Problem locating/validating JTA transaction", t);
    }
    final Object txnIdentifier = jtaPlatform.getTransactionIdentifier(txn);
    Session currentSession = currentSessionMap.get(txnIdentifier);
    if (currentSession == null) {
        currentSession = buildOrObtainSession();
        try {
            txn.registerSynchronization(buildCleanupSynch(txnIdentifier));
        } catch (Throwable t) {
            try {
                currentSession.close();
            } catch (Throwable ignore) {
                LOG.debug("Unable to release generated current-session on failed synch registration", ignore);
            }
            throw new HibernateException("Unable to register cleanup Synchronization with TransactionManager");
        }
        currentSessionMap.put(txnIdentifier, currentSession);
    } else {
        validateExistingSession(currentSession);
    }
    return currentSession;
}
Also used : JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) Transaction(javax.transaction.Transaction) HibernateException(org.hibernate.HibernateException) TransactionManager(javax.transaction.TransactionManager) Session(org.hibernate.Session)

Example 34 with Transaction

use of javax.transaction.Transaction in project hibernate-orm by hibernate.

the class JtaIsolationDelegate method doInSuspendedTransaction.

private <T> T doInSuspendedTransaction(HibernateCallable<T> callable) {
    try {
        // First we suspend any current JTA transaction
        Transaction surroundingTransaction = transactionManager.suspend();
        LOG.debugf("Surrounding JTA transaction suspended [%s]", surroundingTransaction);
        boolean hadProblems = false;
        try {
            return callable.call();
        } catch (HibernateException e) {
            hadProblems = true;
            throw e;
        } finally {
            try {
                transactionManager.resume(surroundingTransaction);
                LOG.debugf("Surrounding JTA transaction resumed [%s]", surroundingTransaction);
            } catch (Throwable t) {
                // if the actually work had an error use that, otherwise error based on t
                if (!hadProblems) {
                    //noinspection ThrowFromFinallyBlock
                    throw new HibernateException("Unable to resume previously suspended transaction", t);
                }
            }
        }
    } catch (SystemException e) {
        throw new HibernateException("Unable to suspend current JTA transaction", e);
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) HibernateException(org.hibernate.HibernateException)

Example 35 with Transaction

use of javax.transaction.Transaction in project Activiti by Activiti.

the class JtaTransactionContext method rollback.

public void rollback() {
    // managed transaction, mark rollback-only if not done so already.
    try {
        Transaction transaction = getTransaction();
        int status = transaction.getStatus();
        if (status != Status.STATUS_NO_TRANSACTION && status != Status.STATUS_ROLLEDBACK) {
            transaction.setRollbackOnly();
        }
    } catch (IllegalStateException e) {
        throw new ActivitiException("Unexpected IllegalStateException while marking transaction rollback only");
    } catch (SystemException e) {
        throw new ActivitiException("SystemException while marking transaction rollback only");
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException)

Aggregations

Transaction (javax.transaction.Transaction)160 SystemException (javax.transaction.SystemException)55 Test (org.junit.Test)42 RollbackException (javax.transaction.RollbackException)26 TransactionManager (javax.transaction.TransactionManager)24 UserTransaction (javax.transaction.UserTransaction)19 NotInTransactionException (org.neo4j.graphdb.NotInTransactionException)14 NotSupportedException (javax.transaction.NotSupportedException)13 Synchronization (javax.transaction.Synchronization)10 XAResource (javax.transaction.xa.XAResource)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)10 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)8 InvalidTransactionException (javax.transaction.InvalidTransactionException)7 TransactionContext (com.hazelcast.transaction.TransactionContext)6 RemoteException (java.rmi.RemoteException)6 ResourceException (javax.resource.ResourceException)6 ManagedConnection (javax.resource.spi.ManagedConnection)6 SQLException (java.sql.SQLException)5 HeuristicMixedException (javax.transaction.HeuristicMixedException)5 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)5