use of cn.taketoday.transaction.TransactionSystemException in project today-infrastructure by TAKETODAY.
the class AbstractTransactionAspectTests method doTestRollbackOnException.
/**
* Check that the given exception thrown by the target can produce the
* desired behavior with the appropriate transaction attribute.
*
* @param ex exception to be thrown by the target
* @param shouldRollback whether this should cause a transaction rollback
*/
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute() {
@Override
public boolean rollbackOn(Throwable t) {
assertThat(t == ex).isTrue();
return shouldRollback;
}
};
Method m = exceptionalMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Gets additional call(s) from TransactionControl
given(ptm.getTransaction(txatt)).willReturn(status);
TransactionSystemException tex = new TransactionSystemException("system exception");
if (rollbackException) {
if (shouldRollback) {
willThrow(tex).given(ptm).rollback(status);
} else {
willThrow(tex).given(ptm).commit(status);
}
}
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
try {
itb.exceptional(ex);
fail("Should have thrown exception");
} catch (Throwable t) {
if (rollbackException) {
assertThat(t).as("Caught wrong exception").isEqualTo(tex);
} else {
assertThat(t).as("Caught wrong exception").isEqualTo(ex);
}
}
if (!rollbackException) {
if (shouldRollback) {
verify(ptm).rollback(status);
} else {
verify(ptm).commit(status);
}
}
}
use of cn.taketoday.transaction.TransactionSystemException in project today-framework by TAKETODAY.
the class AbstractTransactionAspectTests method doTestRollbackOnException.
/**
* Check that the given exception thrown by the target can produce the
* desired behavior with the appropriate transaction attribute.
*
* @param ex exception to be thrown by the target
* @param shouldRollback whether this should cause a transaction rollback
*/
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute() {
@Override
public boolean rollbackOn(Throwable t) {
assertThat(t == ex).isTrue();
return shouldRollback;
}
};
Method m = exceptionalMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Gets additional call(s) from TransactionControl
given(ptm.getTransaction(txatt)).willReturn(status);
TransactionSystemException tex = new TransactionSystemException("system exception");
if (rollbackException) {
if (shouldRollback) {
willThrow(tex).given(ptm).rollback(status);
} else {
willThrow(tex).given(ptm).commit(status);
}
}
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
try {
itb.exceptional(ex);
fail("Should have thrown exception");
} catch (Throwable t) {
if (rollbackException) {
assertThat(t).as("Caught wrong exception").isEqualTo(tex);
} else {
assertThat(t).as("Caught wrong exception").isEqualTo(ex);
}
}
if (!rollbackException) {
if (shouldRollback) {
verify(ptm).rollback(status);
} else {
verify(ptm).commit(status);
}
}
}
use of cn.taketoday.transaction.TransactionSystemException in project today-framework by TAKETODAY.
the class JdbcTransactionObjectSupport method rollbackToSavepoint.
/**
* This implementation rolls back to the given JDBC 3.0 Savepoint.
*
* @see java.sql.Connection#rollback(Savepoint)
*/
@Override
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
ConnectionHolder conHolder = getConnectionHolderForSavepoint();
try {
conHolder.getConnection().rollback((Savepoint) savepoint);
conHolder.resetRollbackOnly();
} catch (Throwable ex) {
throw new TransactionSystemException("Could not roll back to JDBC savepoint", ex);
}
}
use of cn.taketoday.transaction.TransactionSystemException in project today-framework by TAKETODAY.
the class JtaTransactionManager method doCommit.
@Override
protected void doCommit(DefaultTransactionStatus status) {
JtaTransactionObject txObject = (JtaTransactionObject) status.getTransaction();
try {
int jtaStatus = txObject.getUserTransaction().getStatus();
if (jtaStatus == Status.STATUS_NO_TRANSACTION) {
// In any case, the transaction is already fully cleaned up.
throw new UnexpectedRollbackException("JTA transaction already completed - probably rolled back");
}
if (jtaStatus == Status.STATUS_ROLLEDBACK) {
// IllegalStateException expected on JBoss; call still necessary.
try {
txObject.getUserTransaction().rollback();
} catch (IllegalStateException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Rollback failure with transaction already marked as rolled back: {}", ex.toString());
}
}
throw new UnexpectedRollbackException("JTA transaction already rolled back (probably due to a timeout)");
}
txObject.getUserTransaction().commit();
} catch (RollbackException ex) {
throw new UnexpectedRollbackException("JTA transaction unexpectedly rolled back (maybe due to a timeout)", ex);
} catch (HeuristicMixedException ex) {
throw new HeuristicCompletionException(HeuristicCompletionException.STATE_MIXED, ex);
} catch (HeuristicRollbackException ex) {
throw new HeuristicCompletionException(HeuristicCompletionException.STATE_ROLLED_BACK, ex);
} catch (IllegalStateException ex) {
throw new TransactionSystemException("Unexpected internal transaction state", ex);
} catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on commit", ex);
}
}
use of cn.taketoday.transaction.TransactionSystemException in project today-framework by TAKETODAY.
the class JtaTransactionManager method registerAfterCompletionWithExistingTransaction.
@Override
protected void registerAfterCompletionWithExistingTransaction(Object transaction, List<TransactionSynchronization> synchronizations) {
JtaTransactionObject txObject = (JtaTransactionObject) transaction;
logger.debug("Registering after-completion synchronization with existing JTA transaction");
try {
doRegisterAfterCompletionWithJtaTransaction(txObject, synchronizations);
} catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on registerSynchronization", ex);
} catch (Exception ex) {
// Note: JBoss throws plain RuntimeException with RollbackException as cause.
if (ex instanceof RollbackException || ex.getCause() instanceof RollbackException) {
logger.debug("Participating in existing JTA transaction that has been marked for rollback: " + "cannot register Framework after-completion callbacks with outer JTA transaction - " + "immediately performing Framework after-completion callbacks with outcome status 'rollback'. " + "Original exception: {}", ex.toString());
invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK);
} else {
logger.debug("Participating in existing JTA transaction, but unexpected internal transaction " + "state encountered: cannot register Framework after-completion callbacks with outer JTA " + "transaction - processing Framework after-completion callbacks with outcome status 'unknown'" + "Original exception: {}", ex.toString());
invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
}
}
}
Aggregations