private void forgetXAResourceIfRollbacked(XAException ex) throws XAException { if (ex.getCause() != null && ex.getCause() instanceof javax.jms.TransactionRolledBackException) { // The transaction should be forgot to avoid memory leaks if it was rollbacked // post failover recovery. // For example, in ActiveMQ, this should be forgot from ENDED_XA_TRANSACTION_CONTEXTS. getXAResource().forget(xid); } }
/** * @see javax.transaction.UserTransaction#setTransactionTimeout */ public void setTransactionTimeout(int seconds) throws SystemException { try { for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) { ((XAResource) it.next()).setTransactionTimeout(seconds); } } catch (XAException e) { SystemException se = new SystemException( "Unable to set the TransactionTiomeout: XA_ERR=" + e.errorCode); se.initCause(e.getCause()); throw se; } }
/** * @see javax.transaction.UserTransaction#rollback */ public void rollback() throws IllegalStateException, SecurityException, SystemException { if (status != Status.STATUS_ACTIVE && status != Status.STATUS_MARKED_ROLLBACK) { throw new IllegalStateException("Transaction not active"); } try { for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) { XAResource resource = (XAResource) it.next(); XidImpl xid = (XidImpl) xaResources.get(resource); resource.end(xid, XAResource.TMFAIL); } status = Status.STATUS_ROLLING_BACK; for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) { XAResource resource = (XAResource) it.next(); XidImpl xid = (XidImpl) xaResources.get(resource); resource.rollback(xid); } status = Status.STATUS_ROLLEDBACK; } catch (XAException e) { SystemException se = new SystemException( "Unable to rollback transaction: XA_ERR=" + e.errorCode); se.initCause(e.getCause()); throw se; } }
@Test public void testOnePhaseCommitSyncWithRollbackOnlyViaXATerminator() throws Exception { final Xid xid = new XidImple(new Uid()); final Transaction t = SubordinationManager.getTransactionImporter().importTransaction(xid); final TestSynchronization sync = new TestSynchronization(); t.registerSynchronization(sync); t.setRollbackOnly(); final XATerminator xaTerminator = SubordinationManager.getXATerminator(); try { xaTerminator.commit(xid, true); ((TransactionImple)t).doOnePhaseCommit(); fail("did not get expected rollback exception"); } catch(XAException e) { assertEquals("javax.transaction.RollbackException", e.getCause().getClass().getName()); assertEquals(XAException.XA_RBROLLBACK, e.errorCode); // expected - we tried to commit a rollbackonly tx. } assertFalse(sync.isBeforeCompletionDone()); assertTrue(sync.isAfterCompletionDone()); assertEquals(javax.transaction.Status.STATUS_ROLLEDBACK, t.getStatus()); }
@Test public void testOnePhaseCommitSyncWithRollbackOnlyViaXATerminatorWithSeparateSync() throws Exception { final Xid xid = new XidImple(new Uid()); final Transaction t = SubordinationManager.getTransactionImporter().importTransaction(xid); final TestSynchronization sync = new TestSynchronization(); t.registerSynchronization(sync); t.setRollbackOnly(); final XATerminator xaTerminator = SubordinationManager.getXATerminator(); final XATerminatorExtensions xaTerminatorExtensions = (XATerminatorExtensions)xaTerminator; xaTerminatorExtensions.beforeCompletion(xid); try { xaTerminator.commit(xid, true); ((TransactionImple)t).doOnePhaseCommit(); fail("did not get expected rollback exception"); } catch(XAException e) { assertEquals("javax.transaction.RollbackException", e.getCause().getClass().getName()); assertEquals(XAException.XA_RBROLLBACK, e.errorCode); // expected - we tried to commit a rollbackonly tx. } assertTrue(sync.isBeforeCompletionDone()); assertTrue(sync.isAfterCompletionDone()); assertEquals(javax.transaction.Status.STATUS_ROLLEDBACK, t.getStatus()); }
@Test public void testCheckDeferredPrepareInitCause () throws Exception { ThreadActionData.purgeActions(); TransactionImple tx = new TransactionImple(0); tx.enlistResource(new FailureXAResource()); try { tx.enlistResource(new FailureXAResource(FailLocation.prepare, FailType.message)); } catch (final RollbackException ex) { fail(); } try { tx.commit(); fail(); } catch (final RollbackException ex) { assertEquals(XAException.XA_RBROLLBACK, ((XAException) ex.getSuppressed()[0]).errorCode); assertEquals("test message", ((XAException) ex.getSuppressed()[0]).getCause().getMessage()); } }
fail("Should throw an exception here!"); } catch (XAException expected) { assertTrue(expected.getCause().toString().contains("DUPLICATE_ID_REJECTED"));