@Test public void testTxRollbackOnStoreException() throws Exception { PersistenceManagerImpl pm = (PersistenceManagerImpl) TestingUtil.extractComponent(cache, PersistenceManager.class); pm = spy(pm); doThrow(new PersistenceException()).when(pm).prepareAllTxStores(any(), any(), any()); TestingUtil.replaceComponent(cache, PersistenceManager.class, pm, true); TransactionManager tm = TestingUtil.getTransactionManager(cache); Transaction tx = null; try { tm.begin(); tx = tm.getTransaction(); cache.put(KEY1, VAL1); cache.put(KEY2, VAL2); // Throws PersistenceException, forcing the Tx to rollback tm.commit(); } catch (RollbackException e) { // Ensure PersistenceException was the cause of the rollback boolean persistenceEx = false; Throwable[] suppressed = e.getSuppressed(); for (Throwable ex : suppressed) { persistenceEx = ex.getCause() instanceof PersistenceException; if (persistenceEx) break; } assert persistenceEx; } assert tx != null && tx.getStatus() == STATUS_ROLLEDBACK; assertRowCount(0); }
fail("Committed"); } catch (RollbackException e) { assertTrue(Arrays.asList(e.getSuppressed()).contains(rmFail)); assertTrue(Arrays.asList(e.getSuppressed()).contains(rbRollback));
} catch (RollbackException re) { assertTrue("Expected a deferred exception", re.getSuppressed().length > 0); Throwable t = re.getSuppressed()[0];
tm.commit(); } catch (RollbackException e) { Throwable[] suppressed = e.getSuppressed(); assertTrue(suppressed != null && suppressed.length == 1); assertEquals(XAException.class, suppressed[0].getClass());
@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()); } }
} catch (RollbackException e) { e.printStackTrace(); assertTrue(e.getSuppressed()[0] == exception);
@Test public void testCheckDeferredRollbackException () throws Exception { ThreadActionData.purgeActions(); TransactionImple tx = new TransactionImple(0); try { tx.enlistResource(new FailureXAResource(FailLocation.commit, FailType.rollback)); } catch (final RollbackException ex) { fail(); } try { tx.commit(); fail(); } catch (final RollbackException ex) { assertEquals(XAException.XA_HEURRB, ((XAException) ex.getSuppressed()[0]).errorCode); } }
@Test public void testCheckDeferredPrepareRollbackException () throws Exception { ThreadActionData.purgeActions(); TransactionImple tx = new TransactionImple(0); tx.enlistResource(new FailureXAResource()); try { tx.enlistResource(new FailureXAResource(FailLocation.prepare, FailType.rollback)); } catch (final RollbackException ex) { fail(); } try { tx.commit(); fail(); } catch (final RollbackException ex) { assertEquals(XAException.XAER_INVAL, ((XAException) ex.getSuppressed()[0]).errorCode); } }