public void setRollbackOnly() throws IllegalStateException, SystemException { userTransaction.setRollbackOnly(); }
public void setRollbackOnly() throws IllegalStateException, SystemException { userTransaction.setRollbackOnly(); }
/** * Set the underlying JTA transaction to rollback-only. */ private void setRollbackOnlyIfPossible() { if (this.jtaTransaction != null) { try { this.jtaTransaction.setRollbackOnly(); } catch (UnsupportedOperationException ex) { // Probably Hibernate's WebSphereExtendedJTATransactionLookup pseudo JTA stuff... logger.debug("JTA transaction handle does not support setRollbackOnly method - " + "relying on JTA provider to mark the transaction as rollback-only based on " + "the exception thrown from beforeCompletion", ex); } catch (Throwable ex) { logger.error("Could not set JTA transaction rollback-only", ex); } } else { logger.debug("No JTA transaction handle available and/or running on WebLogic - " + "relying on JTA provider to mark the transaction as rollback-only based on " + "the exception thrown from beforeCompletion"); } }
@Override public void markRollbackOnly(){ try { userTransaction.setRollbackOnly(); } catch (SystemException e) { throw new TransactionException( "Unable to mark transaction for rollback only", e ); } }
/** * Set the underlying JTA transaction to rollback-only. */ private void setRollbackOnlyIfPossible() { if (this.jtaTransaction != null) { try { this.jtaTransaction.setRollbackOnly(); } catch (UnsupportedOperationException ex) { // Probably Hibernate's WebSphereExtendedJTATransactionLookup pseudo JTA stuff... logger.debug("JTA transaction handle does not support setRollbackOnly method - " + "relying on JTA provider to mark the transaction as rollback-only based on " + "the exception thrown from beforeCompletion", ex); } catch (Throwable ex) { logger.error("Could not set JTA transaction rollback-only", ex); } } else { logger.debug("No JTA transaction handle available and/or running on WebLogic - " + "relying on JTA provider to mark the transaction as rollback-only based on " + "the exception thrown from beforeCompletion"); } }
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { JtaTransactionObject txObject = (JtaTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Setting JTA transaction rollback-only"); } try { int jtaStatus = txObject.getUserTransaction().getStatus(); if (jtaStatus != Status.STATUS_NO_TRANSACTION && jtaStatus != Status.STATUS_ROLLEDBACK) { txObject.getUserTransaction().setRollbackOnly(); } } catch (IllegalStateException ex) { throw new TransactionSystemException("Unexpected internal transaction state", ex); } catch (SystemException ex) { throw new TransactionSystemException("JTA failure on setRollbackOnly", ex); } }
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { JtaTransactionObject txObject = (JtaTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Setting JTA transaction rollback-only"); } try { int jtaStatus = txObject.getUserTransaction().getStatus(); if (jtaStatus != Status.STATUS_NO_TRANSACTION && jtaStatus != Status.STATUS_ROLLEDBACK) { txObject.getUserTransaction().setRollbackOnly(); } } catch (IllegalStateException ex) { throw new TransactionSystemException("Unexpected internal transaction state", ex); } catch (SystemException ex) { throw new TransactionSystemException("JTA failure on setRollbackOnly", ex); } }
@Test public void jtaTransactionManagerWithIllegalStateExceptionOnRollbackOnly() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); willThrow(new IllegalStateException("no existing transaction")).given(ut).setRollbackOnly(); try { JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { status.setRollbackOnly(); } }); fail("Should have thrown TransactionSystemException"); } catch (TransactionSystemException ex) { // expected } }
@Test public void jtaTransactionManagerWithSystemExceptionOnRollbackOnly() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); willThrow(new SystemException("system exception")).given(ut).setRollbackOnly(); try { JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { status.setRollbackOnly(); TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override public void afterCompletion(int status) { assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN); } }); } }); fail("Should have thrown TransactionSystemException"); } catch (TransactionSystemException ex) { // expected } }
@Test public void jtaTransactionManagerWithExistingTransactionAndSynchronizationNever() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER); ptm.afterPropertiesSet(); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).setRollbackOnly(); }
@Test public void jtaTransactionManagerWithExistingTransactionAndException() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); throw new IllegalStateException("I want a rollback"); } }); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); }
@Test public void jtaTransactionManagerWithExistingTransactionAndRollbackOnly() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); }
@Test public void jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut); ptm.setGlobalRollbackOnParticipationFailure(false); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); }
@Test public void jtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); }
@Test public void jtaTransactionManagerWithExistingAndPropagationSupports() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); }
@Test public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction tx = new MockJtaTransaction(); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(tx); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut, tm); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); assertNotNull(tx.getSynchronization()); tx.getSynchronization().beforeCompletion(); tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); }
@Test public void jtaTransactionManagerWithExistingTransactionAndCommitException() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); final TransactionSynchronization synch = mock(TransactionSynchronization.class); willThrow(new OptimisticLockingFailureException("")).given(synch).beforeCommit(false); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); } }); fail("Should have thrown OptimisticLockingFailureException"); } catch (OptimisticLockingFailureException ex) { // expected } assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); }
@Override public void execute(DelegateExecution execution) throws Exception { EnsureUtil.ensureNotNull("user transaction", utx); // simulate a failure that triggers the transaction rollback utx.setRollbackOnly(); }
@Test public void testMarkedRollback() throws SystemException, NotSupportedException { Assert.assertNotNull(utx); Assert.assertSame(KunderaJTAUserTransaction.class, utx.getClass()); try { utx.setRollbackOnly(); Assert.fail("Should have gone to catch block!"); } catch (IllegalStateException iaex) { Assert.assertEquals("Cannot get Transaction for setRollbackOnly", iaex.getMessage()); } // try after beginning transaction. utx.begin(); utx.setRollbackOnly(); Assert.assertEquals(Status.STATUS_MARKED_ROLLBACK, utx.getStatus()); utx.rollback(); }
/** * Rollback the transaction. */ @Override public void rollback(Throwable e) { if (!commmitted) { try { try { if (userTransaction != null) { if (newTransaction) { userTransaction.rollback(); } else { userTransaction.setRollbackOnly(); } } notifyRollback(e); } finally { closeConnection(); } } catch (Exception ex) { throw new PersistenceException(ex); } } }