Search in sources :

Example 81 with Transaction

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

the class ClientXATest method testRollbackOnTimeout.

@Test
public void testRollbackOnTimeout() throws Exception {
    Hazelcast.newHazelcastInstance();
    HazelcastInstance client = HazelcastClient.newHazelcastClient();
    String name = randomString();
    IQueue<Object> queue = client.getQueue(name);
    queue.offer(randomString());
    HazelcastXAResource xaResource = client.getXAResource();
    tm.setTransactionTimeout(3);
    tm.begin();
    Transaction transaction = tm.getTransaction();
    transaction.enlistResource(xaResource);
    TransactionContext context = xaResource.getTransactionContext();
    try {
        context.getQueue(name).take();
        sleepAtLeastSeconds(5);
        tm.commit();
        fail();
    } catch (RollbackException ignored) {
    // Transaction already rolled-back due to timeout, no need to call tm.rollback explicitly
    }
    assertEquals("Queue size should be 1", 1, queue.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Transaction(javax.transaction.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) RollbackException(javax.transaction.RollbackException) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 82 with Transaction

use of javax.transaction.Transaction in project quickstarts by jboss-switchyard.

the class TaskAServiceBean method doTask.

@Override
public final void doTask(final String command) {
    print("Received command =>  " + command);
    Transaction t = null;
    try {
        t = getCurrentTransaction();
    } catch (Exception e) {
        print("Failed to get current transcation");
    }
    if (t == null) {
        print("No active transaction");
        return;
    }
    _storeA.store(command);
    if (command.contains(ROLLBACK)) {
        try {
            if (++_rollbackCounter % 4 != 0) {
                t.setRollbackOnly();
                print("Marked transaction to rollback!");
            } else {
                print("Rollbacks completed - will be committed");
            }
        } catch (Exception ex) {
            print("Failed to rollback transaction: " + ex.toString());
        }
    }
}
Also used : Transaction(javax.transaction.Transaction)

Example 83 with Transaction

use of javax.transaction.Transaction in project quickstarts by jboss-switchyard.

the class TaskCServiceBean method doTask.

@Override
public final void doTask(final String command) {
    print("Received command =>  " + command);
    Transaction t = null;
    try {
        t = getCurrentTransaction();
    } catch (Exception e) {
        print("Failed to get current transcation");
    }
    _storeC.store(command);
    if (t == null) {
        print("No active transaction");
    } else {
        print("[Error] Managed transaction exists in spite of being marked as " + TransactionPolicy.NO_MANAGED_TRANSACTION);
    }
    if (command.contains(ROLLBACK)) {
        print(String.format("This service requires %s, so it never has transaction to rollback.", TransactionPolicy.NO_MANAGED_TRANSACTION));
    }
}
Also used : Transaction(javax.transaction.Transaction)

Example 84 with Transaction

use of javax.transaction.Transaction in project quickstarts by jboss-switchyard.

the class WorkServiceBean method doWork.

@Override
public final void doWork(final String command) {
    print("Received command =>  " + command);
    Transaction t = null;
    try {
        t = getCurrentTransaction();
    } catch (Exception e) {
        print("Failed to get current transcation");
        return;
    }
    if (t == null) {
        print("No active transaction");
    }
    _taskAService.doTask(command);
    _taskBService.doTask(command);
    _taskCService.doTask(command);
    try {
        t = getCurrentTransaction();
        if (t == null) {
            print("No active transaction");
            return;
        } else if (t.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
            print("transaction is marked as rollback only");
        } else if (t.getStatus() == Status.STATUS_ACTIVE) {
            print("transaction will be committed");
        } else {
            print("Invalid transaction status: " + t.getStatus());
        }
    } catch (Exception e) {
        print("Failed to get current transaction status");
    }
}
Also used : Transaction(javax.transaction.Transaction)

Example 85 with Transaction

use of javax.transaction.Transaction in project neo4j-mobile-android by neo4j-contrib.

the class LockReleaser method addLockToTransaction.

/**
     * Invoking this method with no transaction running will cause the lock to
     * be released right away.
     *
     * @param resource
     *            the resource on which the lock is taken
     * @param type
     *            type of lock (READ or WRITE)
     * @throws NotInTransactionException
     */
public void addLockToTransaction(Object resource, LockType type) throws NotInTransactionException {
    Transaction tx = getTransaction();
    List<LockElement> lockElements = lockMap.get(tx);
    if (lockElements != null) {
        lockElements.add(new LockElement(resource, type));
    } else {
        if (tx == null) {
            // no transaction we release lock right away
            if (type == LockType.WRITE) {
                lockManager.releaseWriteLock(resource, null);
            } else if (type == LockType.READ) {
                lockManager.releaseReadLock(resource, null);
            }
            return;
        }
        lockElements = new ArrayList<LockElement>();
        lockMap.put(tx, lockElements);
        lockElements.add(new LockElement(resource, type));
        // tx was read only
        try {
            tx.registerSynchronization(new ReadOnlyTxReleaser(tx));
        } catch (Exception e) {
            throw new TransactionFailureException("Failed to register lock release synchronization hook", e);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(javax.transaction.Transaction) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) SystemException(javax.transaction.SystemException) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

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