use of javax.transaction.HeuristicRollbackException in project narayana by jbosstm.
the class EnlistResourceDuringCommit method testEnlistResourceDuringBeforeCompletion.
@Test
public void testEnlistResourceDuringBeforeCompletion() throws IllegalStateException, RollbackException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException {
final TransactionImple tx = new TransactionImple(0);
tx.registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
try {
tx.enlistResource(new XAResource() {
@Override
public void start(Xid arg0, int arg1) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
@Override
public boolean setTransactionTimeout(int arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return false;
}
@Override
public void rollback(Xid arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
@Override
public Xid[] recover(int arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return null;
}
@Override
public int prepare(Xid arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return 0;
}
@Override
public boolean isSameRM(XAResource arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return false;
}
@Override
public int getTransactionTimeout() throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return 0;
}
@Override
public void forget(Xid arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
@Override
public void end(Xid arg0, int arg1) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
@Override
public void commit(Xid arg0, boolean arg1) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
});
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RollbackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void afterCompletion(int status) {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
});
tx.commit();
}
use of javax.transaction.HeuristicRollbackException in project narayana by jbosstm.
the class JTATest method testHeuristicRollbackSuppressedException.
@Test
public void testHeuristicRollbackSuppressedException() throws NotSupportedException, SystemException, IllegalStateException, RollbackException, SecurityException, HeuristicMixedException, HeuristicRollbackException {
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.begin();
javax.transaction.Transaction theTransaction = tm.getTransaction();
assertTrue(theTransaction.enlistResource(new XAResource() {
@Override
public void start(Xid xid, int flags) throws XAException {
}
@Override
public void end(Xid xid, int flags) throws XAException {
}
@Override
public int prepare(Xid xid) throws XAException {
return 0;
}
@Override
public void commit(Xid xid, boolean onePhase) throws XAException {
exception = new XAException(XAException.XA_HEURRB);
throw exception;
}
@Override
public void rollback(Xid xid) throws XAException {
}
@Override
public void forget(Xid xid) throws XAException {
}
@Override
public Xid[] recover(int flag) throws XAException {
return null;
}
@Override
public boolean isSameRM(XAResource xaRes) throws XAException {
return false;
}
@Override
public int getTransactionTimeout() throws XAException {
return 0;
}
@Override
public boolean setTransactionTimeout(int seconds) throws XAException {
return false;
}
}));
assertTrue(theTransaction.enlistResource(new XAResource() {
@Override
public void start(Xid xid, int flags) throws XAException {
}
@Override
public void end(Xid xid, int flags) throws XAException {
}
@Override
public int prepare(Xid xid) throws XAException {
return 0;
}
@Override
public void commit(Xid xid, boolean onePhase) throws XAException {
}
@Override
public void rollback(Xid xid) throws XAException {
}
@Override
public void forget(Xid xid) throws XAException {
}
@Override
public Xid[] recover(int flag) throws XAException {
return null;
}
@Override
public boolean isSameRM(XAResource xaRes) throws XAException {
return false;
}
@Override
public int getTransactionTimeout() throws XAException {
return 0;
}
@Override
public boolean setTransactionTimeout(int seconds) throws XAException {
return false;
}
}));
try {
tm.commit();
fail();
} catch (RollbackException e) {
e.printStackTrace();
assertTrue(e.getSuppressed()[0] == exception);
}
}
use of javax.transaction.HeuristicRollbackException in project narayana by jbosstm.
the class TransactionListenerRegistryTest method test.
@Test
public void test() throws SystemException, NotSupportedException, RollbackException, TransactionTypeNotSupported, InterruptedException, InvalidTransactionException, HeuristicRollbackException, HeuristicMixedException {
TransactionManager tm = new TransactionManagerDelegate();
ServerVMClientUserTransaction userTransaction = new ServerVMClientUserTransaction(tm);
userTransaction.setTransactionTimeout(1);
for (CompletionType completionType : CompletionType.values()) {
TransactionListenerRegistry listenerRegistration = (TransactionListenerRegistry) tm;
userTransaction.begin();
// The TSR for interposed synchronizations
final TransactionSynchronizationRegistry tsr = new TransactionSynchronizationRegistryImple();
final TxListener listener = new TxListener(listenerRegistration);
if (completionType != CompletionType.CMTSUSPEND) {
tsr.registerInterposedSynchronization(listener);
} else {
tm.getTransaction().registerSynchronization(listener);
}
listenerRegistration.addListener(tm.getTransaction(), listener, EnumSet.allOf(EventType.class));
if (completionType == CompletionType.CMTSUSPEND) {
Transaction suspended = tm.suspend();
Thread.sleep(2000);
assertTrue(listener.shouldDisassoc());
tm.resume(suspended);
} else {
Thread.sleep(2000);
assertFalse(listener.shouldDisassoc());
}
assertTrue(listener.singleCallAC());
assertTrue(Status.STATUS_ROLLEDBACK == userTransaction.getStatus());
// https://community.jboss.org/thread/92489
if (completionType == CompletionType.BMTCOMMIT) {
try {
userTransaction.commit();
fail("Should not have been able to commit");
} catch (RollbackException e) {
}
} else if (completionType == CompletionType.BMTROLLBACK) {
userTransaction.rollback();
} else if (completionType == CompletionType.CMT) {
// This is possible in CMT mode
// If they did check the status, it is still expected that a CMT
// calls suspend at least when a tx is marked as completed to
// clear
// it from the thread
tm.suspend();
}
assertTrue(listener.singleCallAC());
if (completionType == CompletionType.CMTSUSPEND) {
assertFalse(listener.shouldDisassoc());
} else {
assertTrue(listener.shouldDisassoc());
}
assertTrue(listener.isClosed());
}
}
use of javax.transaction.HeuristicRollbackException in project narayana by jbosstm.
the class WedgedResourceDemonstrator method testWedge.
@Test
public void testWedge() throws InvalidName, SystemException, NotSupportedException, javax.transaction.SystemException, IllegalStateException, RollbackException, SecurityException, HeuristicMixedException, HeuristicRollbackException, InterruptedException {
String mode = "jts";
if (mode.equals("jts")) {
ORB myORB = ORB.getInstance("test");
RootOA myOA = OA.getRootOA(myORB);
myORB.initORB(new String[0], null);
myOA.initOA();
com.arjuna.ats.internal.jts.ORBManager.setORB(myORB);
com.arjuna.ats.internal.jts.ORBManager.setPOA(myOA);
RecoveryManager.manager().initialize();
}
TransactionManager transactionManager = mode.equals("jts") ? new com.arjuna.ats.internal.jta.transaction.jts.TransactionManagerImple() : new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple();
transactionManager.setTransactionTimeout(2);
transactionManager.begin();
transactionManager.getTransaction().enlistResource(new TimeoutOnFirstRollbackResource());
// Business logic
Thread.currentThread().sleep(5000);
try {
transactionManager.commit();
fail("Should not have been able to commit");
} catch (RollbackException e) {
// This is fine
} catch (IllegalStateException e) {
// This is fine
} finally {
if (mode.equals("jts")) {
RecoveryManager.manager().terminate();
ORB myORB = ORB.getInstance("test");
RootOA myOA = OA.getRootOA(myORB);
myOA.destroy();
myORB.shutdown();
}
}
}
use of javax.transaction.HeuristicRollbackException in project ovirt-engine by oVirt.
the class TransactionSupport method executeInNewTransaction.
/**
* Forces "REQUIRES_NEW" and executes given code in that scope
*/
public static <T> T executeInNewTransaction(TransactionMethod<T> code) {
T result = null;
Transaction transaction = null;
try {
TransactionManager tm = findTransactionManager();
// suspend existing if exists
transaction = tm.getTransaction();
if (transaction != null) {
transaction = tm.suspend();
}
// start new transaction
tm.begin();
Transaction newTransaction = tm.getTransaction();
// run the code
try {
result = code.runInTransaction();
} catch (RuntimeException rte) {
tm.rollback();
log.info("transaction rolled back");
throw rte;
} catch (Exception e) {
// code failed need to rollback
tm.rollback();
log.info("transaction rolled back");
log.error("executeInNewTransaction - Wrapping Exception: {} with RunTimeException", e.getClass().getName());
throw new RuntimeException("Failed executing code", e);
}
// commit or rollback according to state
if (needToRollback(newTransaction.getStatus())) {
tm.rollback();
} else {
tm.commit();
}
} catch (SystemException | NotSupportedException | HeuristicRollbackException | HeuristicMixedException | RollbackException | IllegalStateException | SecurityException e) {
throw new RuntimeException("Failed managing transaction", e);
} finally {
// check if we need to resume previous transaction
if (transaction != null) {
resume(transaction);
}
}
// and we are done...
return result;
}
Aggregations