@Test void unitOfWorkIsRolledBackWhenTransactionFailsToStart() { TransactionManager transactionManager = mock(TransactionManager.class); when(transactionManager.startTransaction()).thenThrow(new MockException()); try { subject.attachTransaction(transactionManager); fail("Expected MockException to be propagated"); } catch (Exception e) { } verify(subject).rollback(isA(MockException.class)); }
@Override public void rollback(Throwable cause) { if (logger.isDebugEnabled()) { logger.debug("Rolling back Unit Of Work.", cause); } Assert.state(isActive() && phase().isBefore(Phase.ROLLBACK), () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { setRollbackCause(cause); changePhase(Phase.ROLLBACK); if (isRoot()) { changePhase(Phase.CLEANUP, Phase.CLOSED); } } finally { CurrentUnitOfWork.clear(this); } }
AbstractUnitOfWork implements UnitOfWork<T> { @Override public void rollback(Throwable cause) { if (logger.isDebugEnabled()) { logger.debug("Rolling back Unit Of Work.", cause); } Assert.state(isActive() && phase().isBefore(Phase.ROLLBACK), () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { setRollbackCause(cause); changePhase(Phase.ROLLBACK); if (isRoot()) { changePhase(Phase.CLEANUP, Phase.CLOSED); } } finally { CurrentUnitOfWork.clear(this); } } }
AbstractUnitOfWork implements UnitOfWork<T> { @Override public void rollback(Throwable cause) { if (logger.isDebugEnabled()) { logger.debug("Rolling back Unit Of Work.", cause); } Assert.state(isActive() && phase().isBefore(Phase.ROLLBACK), () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { setRollbackCause(cause); changePhase(Phase.ROLLBACK); if (isRoot()) { changePhase(Phase.CLEANUP, Phase.CLOSED); } } finally { CurrentUnitOfWork.clear(this); } }  }
AbstractUnitOfWork implements UnitOfWork<T> { @Override public void rollback(Throwable cause) { if (logger.isDebugEnabled()) { logger.debug("Rolling back Unit Of Work.", cause); } Assert.state(isActive() && phase().isBefore(Phase.ROLLBACK), () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { setRollbackCause(cause); changePhase(Phase.ROLLBACK); if (isRoot()) { changePhase(Phase.CLEANUP, Phase.CLOSED); } } finally { CurrentUnitOfWork.clear(this); } }  @Override void start(); @Override void commit(); @Override void rollback(Throwable cause); @Override Optional<UnitOfWork<?>> parent(); @Override Map<String, Object> resources(); @Override boolean isRolledBack(); @Override void registerCorrelationDataProvider(CorrelationDataProvider correlationDataProvider); @Override MetaData getCorrelationData(); @Override void onPrepareCommit(Consumer<UnitOfWork<T>> handler); @Override void onCommit(Consumer<UnitOfWork<T>> handler); @Override void afterCommit(Consumer<UnitOfWork<T>> handler); @Override void onRollback(Consumer<UnitOfWork<T>> handler); @Override void onCleanup(Consumer<UnitOfWork<T>> handler); @Override Phase phase(); }
AbstractUnitOfWork implements UnitOfWork<T> { @Override public void rollback(Throwable cause) { if (logger.isDebugEnabled()) { logger.debug("Rolling back Unit Of Work.", cause); } Assert.state(isActive() && phase().isBefore(Phase.ROLLBACK), () -> String.format("The UnitOfWork is in an incompatible phase: %s", phase())); Assert.state(isCurrent(), () -> "The UnitOfWork is not the current Unit of Work"); try { setRollbackCause(cause); changePhase(Phase.ROLLBACK); if (isRoot()) { changePhase(Phase.CLEANUP, Phase.CLOSED); } } finally { CurrentUnitOfWork.clear(this); } }  @Override void start(); @Override void commit(); @Override void rollback(Throwable cause); @Override Optional<UnitOfWork<?>> parent(); @Override Map<String, Object> resources(); @Override boolean isRolledBack(); @Override void registerCorrelationDataProvider(CorrelationDataProvider correlationDataProvider); @Override MetaData getCorrelationData(); @Override void onPrepareCommit(Consumer<UnitOfWork<T>> handler); @Override void onCommit(Consumer<UnitOfWork<T>> handler); @Override void afterCommit(Consumer<UnitOfWork<T>> handler); @Override void onRollback(Consumer<UnitOfWork<T>> handler); @Override void onCleanup(Consumer<UnitOfWork<T>> handler); @Override Phase phase();  }