// OptimisticLockException thrown here means that a change happened while AnyEntity was still "checked out" try { tx2.commit(); em2.close(); } catch (RollbackException ex ) { Throwable cause = ex.getCause(); if (cause != null && cause instanceof OptimisticLockException) { System.out.println("Someone already changed AnyEntity."); } else { throw ex; } }
try { entityManager.getTransaction().begin(); ... entityManager.getTransaction().commit(); } catch (RollbackException e1) { System.out.println(e1.getMessage()); System.out.println(e1.getCause()); throw ...; }
/** * Rollback resources after a phase 1 prepare failure. All resources must be rolled back as prepared ones * are in-doubt and non-prepared ones have started/ended work done that must also be cleaned. * @param rbEx the thrown rollback exception. * @throws BitronixSystemException when a resource could not rollback prepapared state. */ private void rollbackPrepareFailure(RollbackException rbEx) throws BitronixSystemException { List<XAResourceHolderState> interestedResources = resourceManager.getAllResources(); try { rollbacker.rollback(this, interestedResources); if (log.isDebugEnabled()) { log.debug("rollback after prepare failure succeeded"); } } catch (Exception ex) { // let's merge both exceptions' PhaseException to report a complete error message PhaseException preparePhaseEx = (PhaseException) rbEx.getCause(); PhaseException rollbackPhaseEx = (PhaseException) ex.getCause(); List<Exception> exceptions = new ArrayList<Exception>(); List<XAResourceHolderState> resources = new ArrayList<XAResourceHolderState>(); exceptions.addAll(preparePhaseEx.getExceptions()); exceptions.addAll(rollbackPhaseEx.getExceptions()); resources.addAll(preparePhaseEx.getResourceStates()); resources.addAll(rollbackPhaseEx.getResourceStates()); throw new BitronixSystemException("transaction partially prepared and only partially rolled back. Some resources might be left in doubt!", new PhaseException(exceptions, resources)); } }
/** * Rollback resources after a phase 1 prepare failure. All resources must be rolled back as prepared ones * are in-doubt and non-prepared ones have started/ended work done that must also be cleaned. * @param rbEx the thrown rollback exception. * @throws BitronixSystemException when a resource could not rollback prepapared state. */ private void rollbackPrepareFailure(RollbackException rbEx) throws BitronixSystemException { List<XAResourceHolderState> interestedResources = resourceManager.getAllResources(); try { rollbacker.rollback(this, interestedResources); if (log.isDebugEnabled()) log.debug("rollback after prepare failure succeeded"); } catch (Exception ex) { // let's merge both exceptions' PhaseException to report a complete error message PhaseException preparePhaseEx = (PhaseException) rbEx.getCause(); PhaseException rollbackPhaseEx = (PhaseException) ex.getCause(); List<Exception> exceptions = new ArrayList<Exception>(); List<XAResourceHolderState> resources = new ArrayList<XAResourceHolderState>(); exceptions.addAll(preparePhaseEx.getExceptions()); exceptions.addAll(rollbackPhaseEx.getExceptions()); resources.addAll(preparePhaseEx.getResourceStates()); resources.addAll(rollbackPhaseEx.getResourceStates()); throw new BitronixSystemException("transaction partially prepared and only partially rolled back. Some resources might be left in doubt!", new PhaseException(exceptions, resources)); } }
/** * Rollback resources after a phase 1 prepare failure. All resources must be rolled back as prepared ones * are in-doubt and non-prepared ones have started/ended work done that must also be cleaned. * @param rbEx the thrown rollback exception. * @throws BitronixSystemException when a resource could not rollback prepapared state. */ private void rollbackPrepareFailure(RollbackException rbEx) throws BitronixSystemException { List<XAResourceHolderState> interestedResources = resourceManager.getAllResources(); try { rollbacker.rollback(this, interestedResources); if (log.isDebugEnabled()) log.debug("rollback after prepare failure succeeded"); } catch (Exception ex) { // let's merge both exceptions' PhaseException to report a complete error message PhaseException preparePhaseEx = (PhaseException) rbEx.getCause(); PhaseException rollbackPhaseEx = (PhaseException) ex.getCause(); List<Exception> exceptions = new ArrayList<Exception>(); List<XAResourceHolderState> resources = new ArrayList<XAResourceHolderState>(); exceptions.addAll(preparePhaseEx.getExceptions()); exceptions.addAll(rollbackPhaseEx.getExceptions()); resources.addAll(preparePhaseEx.getResourceStates()); resources.addAll(rollbackPhaseEx.getResourceStates()); throw new BitronixSystemException("transaction partially prepared and only partially rolled back. Some resources might be left in doubt!", new PhaseException(exceptions, resources)); } }
/** * Rollback resources after a phase 1 prepare failure. All resources must be rolled back as prepared ones * are in-doubt and non-prepared ones have started/ended work done that must also be cleaned. * @param rbEx the thrown rollback exception. * @throws BitronixSystemException when a resource could not rollback prepapared state. */ private void rollbackPrepareFailure(RollbackException rbEx) throws BitronixSystemException { List<XAResourceHolderState> interestedResources = resourceManager.getAllResources(); try { rollbacker.rollback(this, interestedResources); if (log.isDebugEnabled()) { log.debug("rollback after prepare failure succeeded"); } } catch (Exception ex) { // let's merge both exceptions' PhaseException to report a complete error message PhaseException preparePhaseEx = (PhaseException) rbEx.getCause(); PhaseException rollbackPhaseEx = (PhaseException) ex.getCause(); List<Exception> exceptions = new ArrayList<Exception>(); List<XAResourceHolderState> resources = new ArrayList<XAResourceHolderState>(); exceptions.addAll(preparePhaseEx.getExceptions()); exceptions.addAll(rollbackPhaseEx.getExceptions()); resources.addAll(preparePhaseEx.getResourceStates()); resources.addAll(rollbackPhaseEx.getResourceStates()); throw new BitronixSystemException("transaction partially prepared and only partially rolled back. Some resources might be left in doubt!", new PhaseException(exceptions, resources)); } }
public void testBeforeCompletionRuntimeExceptionRethrown() throws Exception { btm.begin(); btm.getTransaction().registerSynchronization(new Synchronization() { public void beforeCompletion() { throw new RuntimeException("beforeCompletion failure"); } public void afterCompletion(int i) { } }); try { btm.commit(); fail("expected runtime exception"); } catch (RollbackException ex) { assertEquals(RuntimeException.class, ex.getCause().getClass()); assertEquals("beforeCompletion failure", ex.getCause().getMessage()); } btm.begin(); btm.commit(); }
public void testBeforeCompletionRuntimeExceptionRethrown() throws Exception { btm.begin(); btm.getTransaction().registerSynchronization(new Synchronization() { public void beforeCompletion() { throw new RuntimeException("beforeCompletion failure"); } public void afterCompletion(int i) { } }); try { btm.commit(); fail("expected runtime exception"); } catch (RollbackException ex) { assertEquals(RuntimeException.class, ex.getCause().getClass()); assertEquals("beforeCompletion failure", ex.getCause().getMessage()); } btm.begin(); btm.commit(); }
public String updateKeyValueDatabase(String key, String value) { EntityManager entityManager = entityManagerFactory.createEntityManager(); try { userTransaction.begin(); entityManager.joinTransaction(); String result = updateKeyValueDatabase(entityManager, key, value); userTransaction.commit(); return result; } catch (RollbackException e) { Throwable t = e.getCause(); return t != null ? t.getMessage() : e.getMessage(); } catch (Exception e) { return e.getMessage(); } finally { try { if (userTransaction.getStatus() == Status.STATUS_ACTIVE) userTransaction.rollback(); } catch (Throwable e) { } entityManager.close(); } }
fail("Commit should have thrown RollbackException"); } catch(RollbackException re) { Throwable cause = re.getCause(); assertNotNull(cause); assertTrue(cause instanceof SynchronizationCommitConflictException);
assertTrue(ex.getMessage().matches("transaction failed to prepare: a Bitronix Transaction with GTRID (.*?) status=ROLLEDBACK, 3 resource\\(s\\) enlisted (.*?)")); assertTrue(ex.getCause().getMessage().matches("transaction failed during prepare of a Bitronix Transaction with GTRID (.*?), status=PREPARING, 3 resource\\(s\\) enlisted (.*?): resource\\(s\\) \\[pds2\\] threw unexpected exception")); " [pds2 - javax.transaction.xa.XAException(XAER_RMERR) - resource 2 prepare failed]", ex.getCause().getCause().getMessage());
assertTrue(ex.getMessage().matches("transaction failed to prepare: a Bitronix Transaction with GTRID (.*?) status=ROLLEDBACK, 3 resource\\(s\\) enlisted (.*?)")); assertTrue(ex.getCause().getMessage().matches("transaction failed during prepare of a Bitronix Transaction with GTRID (.*?), status=PREPARING, 3 resource\\(s\\) enlisted (.*?): resource\\(s\\) \\[pds2\\] threw unexpected exception")); " [pds2 - javax.transaction.xa.XAException(XAER_RMERR) - resource 2 prepare failed]", ex.getCause().getCause().getMessage());
throw new GenericTransactionException("Roll back error, could not commit transaction, was rolled back instead because of: " + rollbackOnlyCause.getCauseMessage(), rollbackOnlyCause.getCauseThrowable()); Throwable t = e.getCause() == null ? e : e.getCause(); throw new GenericTransactionException("Roll back error (with no rollbackOnly cause found), could not commit transaction, was rolled back instead: " + t.toString(), t); } catch (IllegalStateException e) {
} catch (RollbackException ex) { assertTrue("Got: " + ex.getMessage(), ex.getMessage().matches("transaction failed to prepare: a Bitronix Transaction with GTRID (.*?) status=ROLLEDBACK, 2 resource\\(s\\) enlisted (.*?)")); assertTrue("Got: " + ex.getCause().getMessage(), ex.getCause().getMessage().matches("transaction failed during prepare of a Bitronix Transaction with GTRID (.*?) status=PREPARING, 2 resource\\(s\\) enlisted (.*?) resource\\(s\\) \\[pds1\\] threw unexpected exception")); " [pds1 - java.lang.RuntimeException - driver error]", ex.getCause().getCause().getMessage());
assertTrue(ex.getMessage().matches("transaction failed to prepare: a Bitronix Transaction with GTRID (.*?) status=ROLLEDBACK, 2 resource\\(s\\) enlisted (.*?)")); assertTrue(ex.getCause().getMessage().matches("transaction failed during prepare of a Bitronix Transaction with GTRID (.*?), status=PREPARING, 2 resource\\(s\\) enlisted (.*?): resource\\(s\\) \\[pds1\\] threw unexpected exception")); " [pds1 - javax.transaction.xa.XAException(XAER_RMERR) - resource 1 prepare failed]", ex.getCause().getCause().getMessage());
} catch (RollbackException ex) { assertTrue("Got: " + ex.getMessage(), ex.getMessage().matches("transaction failed to prepare: a Bitronix Transaction with GTRID (.*?) status=ROLLEDBACK, 2 resource\\(s\\) enlisted (.*?)")); assertTrue("Got: " + ex.getCause().getMessage(), ex.getCause().getMessage().matches("transaction failed during prepare of a Bitronix Transaction with GTRID (.*?) status=PREPARING, 2 resource\\(s\\) enlisted (.*?) resource\\(s\\) \\[pds1\\] threw unexpected exception")); " [pds1 - java.lang.RuntimeException - driver error]", ex.getCause().getCause().getMessage());
} catch (RollbackException ex) { assertTrue("Got: " + ex.getMessage(), ex.getMessage().matches("transaction failed to prepare: a Bitronix Transaction with GTRID (.*?) status=ROLLEDBACK, 2 resource\\(s\\) enlisted (.*?)")); assertTrue("Got: " + ex.getCause().getMessage(), ex.getCause().getMessage().matches("transaction failed during prepare of a Bitronix Transaction with GTRID (.*?) status=PREPARING, 2 resource\\(s\\) enlisted (.*?) resource\\(s\\) \\[pds1\\] threw unexpected exception")); " [pds1 - javax.transaction.xa.XAException(XAER_RMERR) - resource failed]", ex.getCause().getCause().getMessage());
} catch (RollbackException ex) { assertTrue("Got: " + ex.getMessage(), ex.getMessage().matches("transaction failed to prepare: a Bitronix Transaction with GTRID (.*?) status=ROLLEDBACK, 2 resource\\(s\\) enlisted (.*?)")); assertTrue("Got: " + ex.getCause().getMessage(), ex.getCause().getMessage().matches("transaction failed during prepare of a Bitronix Transaction with GTRID (.*?) status=PREPARING, 2 resource\\(s\\) enlisted (.*?) resource\\(s\\) \\[pds1\\] threw unexpected exception")); " [pds1 - javax.transaction.xa.XAException(XAER_RMERR) - resource failed]", ex.getCause().getCause().getMessage());
assertTrue(ex.getMessage().matches("transaction failed to prepare: a Bitronix Transaction with GTRID \\[.*\\], status=ROLLEDBACK, 2 resource\\(s\\) enlisted .*")); assertEquals("collected 1 exception(s):" + System.getProperty("line.separator") + " [pds2 - javax.transaction.xa.XAException(XAER_RMERR) - resource 2 prepare failed]", ex.getCause().getCause().getMessage());
transactionManager.commit(); } catch (RollbackException re) { if (re.getCause() instanceof SetRollbackOnlyException) {