use of jakarta.transaction.RollbackException in project tomcat by apache.
the class TransactionContext method addTransactionContextListener.
/**
* Adds a listener for transaction completion events.
*
* @param listener
* the listener to add
* @throws SQLException
* if a problem occurs adding the listener to the transaction
*/
public void addTransactionContextListener(final TransactionContextListener listener) throws SQLException {
try {
if (!isActive()) {
final Transaction transaction = this.transactionRef.get();
listener.afterCompletion(TransactionContext.this, transaction != null && transaction.getStatus() == Status.STATUS_COMMITTED);
return;
}
final Synchronization s = new Synchronization() {
@Override
public void afterCompletion(final int status) {
listener.afterCompletion(TransactionContext.this, status == Status.STATUS_COMMITTED);
}
@Override
public void beforeCompletion() {
// empty
}
};
if (transactionSynchronizationRegistry != null) {
transactionSynchronizationRegistry.registerInterposedSynchronization(s);
} else {
getTransaction().registerSynchronization(s);
}
} catch (final RollbackException e) {
// JTA spec doesn't let us register with a transaction marked rollback only
// just ignore this and the tx state will be cleared another way.
} catch (final Exception e) {
throw new SQLException("Unable to register transaction context listener", e);
}
}
use of jakarta.transaction.RollbackException in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackExceptionOnCommit.
@Test
public void jtaTransactionManagerWithRollbackExceptionOnCommit() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new RollbackException("unexpected rollback")).given(ut).commit();
assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCompletion(int status) {
assertThat(status == TransactionSynchronization.STATUS_ROLLED_BACK).as("Correct completion status").isTrue();
}
});
}
});
});
verify(ut).begin();
}
use of jakarta.transaction.RollbackException in project spring-framework by spring-projects.
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);
}
}
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 jakarta.transaction.RollbackException in project tomcat by apache.
the class TransactionContext method setSharedConnection.
/**
* Sets the shared connection for this transaction. The shared connection is enlisted in the transaction.
*
* @param sharedConnection
* the shared connection
* @throws SQLException
* if a shared connection is already set, if XAResource for the connection could not be found in the
* transaction registry, or if there was a problem enlisting the connection in the transaction
*/
public void setSharedConnection(final Connection sharedConnection) throws SQLException {
if (this.sharedConnection != null) {
throw new IllegalStateException("A shared connection is already set");
}
// This is the first use of the connection in this transaction, so we must
// enlist it in the transaction
final Transaction transaction = getTransaction();
try {
final XAResource xaResource = transactionRegistry.getXAResource(sharedConnection);
if (!transaction.enlistResource(xaResource)) {
throw new SQLException("Unable to enlist connection in transaction: enlistResource returns 'false'.");
}
} catch (final IllegalStateException e) {
// This can happen if the transaction is already timed out
throw new SQLException("Unable to enlist connection in the transaction", e);
} catch (final RollbackException e) {
// transaction was rolled back... proceed as if there never was a transaction
} catch (final SystemException e) {
throw new SQLException("Unable to enlist connection the transaction", e);
}
this.sharedConnection = sharedConnection;
}
use of jakarta.transaction.RollbackException in project spring-framework by spring-projects.
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 Spring after-completion callbacks with outer JTA transaction - " + "immediately performing Spring after-completion callbacks with outcome status 'rollback'. " + "Original exception: " + ex);
invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK);
} else {
logger.debug("Participating in existing JTA transaction, but unexpected internal transaction " + "state encountered: cannot register Spring after-completion callbacks with outer JTA " + "transaction - processing Spring after-completion callbacks with outcome status 'unknown'" + "Original exception: " + ex);
invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
}
}
}
Aggregations