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());
}
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());
}
}
}
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));
}
}
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");
}
}
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);
}
}
}
Aggregations