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;
}
use of javax.transaction.HeuristicRollbackException in project Payara by payara.
the class JavaEETransactionImpl method commit.
@Override
public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
checkTransationActive();
// If this transaction is set for timeout, cancel it as it is in the commit state
if (isTimerTask) {
cancelTimerTask();
}
// END local transaction timeout
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "--In JavaEETransactionImpl.commit, jtsTx=" + jtsTx + " nonXAResource=" + nonXAResource);
}
commitStarted = true;
boolean success = false;
if (jtsTx != null) {
try {
jtsTx.commit();
success = true;
} catch (HeuristicMixedException e) {
success = true;
throw e;
} finally {
((JavaEETransactionManagerSimplified) javaEETM).monitorTxCompleted(this, success);
((JavaEETransactionManagerSimplified) javaEETM).clearThreadTx();
onTxCompletion(success);
try {
localTxStatus = jtsTx.getStatus();
} catch (Exception e) {
localTxStatus = Status.STATUS_NO_TRANSACTION;
}
jtsTx = null;
}
} else {
// local tx
Exception caughtException = null;
try {
if (timedOut) {
// rollback nonXA resource
if (nonXAResource != null) {
nonXAResource.getXAResource().rollback(xid);
}
localTxStatus = Status.STATUS_ROLLEDBACK;
throw new RollbackException(sm.getString("enterprise_distributedtx.rollback_timeout"));
}
if (isRollbackOnly()) {
// rollback nonXA resource
if (nonXAResource != null) {
nonXAResource.getXAResource().rollback(xid);
}
localTxStatus = Status.STATUS_ROLLEDBACK;
throw new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
}
// call beforeCompletion
for (int i = 0; i < syncs.size(); i++) {
try {
Synchronization sync = (Synchronization) syncs.elementAt(i);
sync.beforeCompletion();
} catch (RuntimeException ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
setRollbackOnly();
caughtException = ex;
break;
} catch (Exception ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
// XXX-V2 no setRollbackOnly() ???
}
}
for (int i = 0; i < interposedSyncs.size(); i++) {
try {
Synchronization sync = (Synchronization) interposedSyncs.elementAt(i);
sync.beforeCompletion();
} catch (RuntimeException ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
setRollbackOnly();
caughtException = ex;
break;
} catch (Exception ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
// XXX-V2 no setRollbackOnly() ???
}
}
// calls marked it for rollback.
if (isRollbackOnly()) {
// Check if it is a Local Transaction
RollbackException rbe = null;
if (jtsTx == null) {
if (nonXAResource != null) {
nonXAResource.getXAResource().rollback(xid);
}
localTxStatus = Status.STATUS_ROLLEDBACK;
rbe = new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
// else it is a global transaction
} else {
jtsTx.rollback();
localTxStatus = Status.STATUS_ROLLEDBACK;
rbe = new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
}
// RollbackException doesn't have a constructor that takes a Throwable.
if (caughtException != null) {
rbe.initCause(caughtException);
}
throw rbe;
}
// beforeCompletions registered the first XA resource.
if (jtsTx != null) {
jtsTx.commit();
// Note: JTS will not call afterCompletions in this case,
// because no syncs have been registered with JTS.
// So afterCompletions are called in finally block below.
} else {
// do single-phase commit on nonXA resource
if (nonXAResource != null) {
nonXAResource.getXAResource().commit(xid, true);
}
}
// V2-XXX should this be STATUS_NO_TRANSACTION ?
localTxStatus = Status.STATUS_COMMITTED;
success = true;
} catch (RollbackException ex) {
// V2-XXX is this correct ?
localTxStatus = Status.STATUS_ROLLEDBACK;
throw ex;
} catch (SystemException ex) {
// localTxStatus = Status.STATUS_ROLLEDBACK; // V2-XXX is this correct ?
localTxStatus = Status.STATUS_COMMITTING;
success = true;
throw ex;
} catch (Exception ex) {
// V2-XXX is this correct ?
localTxStatus = Status.STATUS_ROLLEDBACK;
SystemException exc = new SystemException();
exc.initCause(ex);
throw exc;
} finally {
((JavaEETransactionManagerSimplified) javaEETM).monitorTxCompleted(this, success);
((JavaEETransactionManagerSimplified) javaEETM).clearThreadTx();
for (int i = 0; i < interposedSyncs.size(); i++) {
try {
Synchronization sync = (Synchronization) interposedSyncs.elementAt(i);
sync.afterCompletion(localTxStatus);
} catch (Exception ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.after_completion_excep", ex);
}
}
// call afterCompletions
for (int i = 0; i < syncs.size(); i++) {
try {
Synchronization sync = (Synchronization) syncs.elementAt(i);
sync.afterCompletion(localTxStatus);
} catch (Exception ex) {
_logger.log(Level.WARNING, "enterprise_distributedtx.after_completion_excep", ex);
}
}
onTxCompletion(success);
jtsTx = null;
}
}
}
use of javax.transaction.HeuristicRollbackException in project wildfly by wildfly.
the class DatabaseTimerPersistence method shouldRun.
@Override
public boolean shouldRun(TimerImpl timer) {
final ContextTransactionManager tm = ContextTransactionManager.getInstance();
if (!allowExecution) {
// timers never execute on this node
return false;
}
String loadTimer = sql.getProperty(UPDATE_RUNNING);
Connection connection = null;
PreparedStatement statement = null;
try {
tm.begin();
try {
connection = dataSource.getConnection();
statement = connection.prepareStatement(loadTimer);
statement.setString(1, TimerState.IN_TIMEOUT.name());
setNodeName(TimerState.IN_TIMEOUT, statement, 2);
statement.setString(3, timer.getId());
statement.setString(4, TimerState.IN_TIMEOUT.name());
statement.setString(5, TimerState.RETRY_TIMEOUT.name());
if (timer.getNextExpiration() == null) {
statement.setTimestamp(6, null);
} else {
statement.setTimestamp(6, timestamp(timer.getNextExpiration()));
}
} catch (SQLException e) {
try {
tm.rollback();
} catch (Exception ee) {
EjbLogger.EJB3_TIMER_LOGGER.timerUpdateFailedAndRollbackNotPossible(ee);
}
// fix for WFLY-10130
EjbLogger.EJB3_TIMER_LOGGER.exceptionCheckingIfTimerShouldRun(timer, e);
return false;
}
int affected = statement.executeUpdate();
tm.commit();
return affected == 1;
} catch (SQLException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
// failed to update the DB
try {
tm.rollback();
} catch (IllegalStateException | SecurityException | SystemException rbe) {
EjbLogger.EJB3_TIMER_LOGGER.timerUpdateFailedAndRollbackNotPossible(rbe);
}
EjbLogger.EJB3_TIMER_LOGGER.debugf(e, "Timer %s not running due to exception ", timer);
return false;
} catch (NotSupportedException e) {
// happen from tm.begin, no rollback necessary
EjbLogger.EJB3_TIMER_LOGGER.timerNotRunning(e, timer);
return false;
} finally {
safeClose(statement);
safeClose(connection);
}
}
use of javax.transaction.HeuristicRollbackException in project wildfly by wildfly.
the class BeforeCompletionExceptionDestroysBeanTestCase method testExceptionInBeforeCompletionDestroysBean.
@Test
public void testExceptionInBeforeCompletionDestroysBean() throws NamingException, SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException {
final UserTransaction userTransaction = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
final BeforeCompletionSFSB bean = (BeforeCompletionSFSB) new InitialContext().lookup("java:module/" + BeforeCompletionSFSB.class.getSimpleName());
// begin a transaction
userTransaction.begin();
bean.enlist();
// commit, this should destroy the bean, as it's @BeforeCompletion method will throw an exception
try {
userTransaction.commit();
} catch (RollbackException expected) {
}
try {
userTransaction.begin();
bean.enlist();
throw new RuntimeException("Expected SFSB to be destroyed");
} catch (NoSuchEJBException expected) {
} finally {
userTransaction.rollback();
}
}
Aggregations