use of jakarta.transaction.SystemException in project tomcat by apache.
the class TransactionContext method isActive.
/**
* True if the transaction is active or marked for rollback only.
*
* @return true if the transaction is active or marked for rollback only; false otherwise
* @throws SQLException
* if a problem occurs obtaining the transaction status
*/
public boolean isActive() throws SQLException {
try {
final Transaction transaction = this.transactionRef.get();
if (transaction == null) {
return false;
}
final int status = transaction.getStatus();
return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
} catch (final SystemException e) {
throw new SQLException("Unable to get transaction status", e);
}
}
use of jakarta.transaction.SystemException in project tomcat by apache.
the class TransactionRegistry method getActiveTransactionContext.
/**
* Gets the active TransactionContext or null if not Transaction is active.
*
* @return The active TransactionContext or null if no Transaction is active.
* @throws SQLException
* Thrown when an error occurs while fetching the transaction.
*/
public TransactionContext getActiveTransactionContext() throws SQLException {
Transaction transaction = null;
try {
transaction = transactionManager.getTransaction();
// was there a transaction?
if (transaction == null) {
return null;
}
// This is the transaction on the thread so no need to check it's status - we should try to use it and
// fail later based on the subsequent status
} catch (final SystemException e) {
throw new SQLException("Unable to determine current transaction ", e);
}
// register the context (or create a new one)
synchronized (this) {
TransactionContext cache = caches.get(transaction);
if (cache == null) {
cache = new TransactionContext(this, transaction, transactionSynchronizationRegistry);
caches.put(transaction, cache);
}
return cache;
}
}
use of jakarta.transaction.SystemException in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnBegin.
@Test
public void jtaTransactionManagerWithSystemExceptionOnBegin() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
willThrow(new SystemException("system exception")).given(ut).begin();
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
});
}
use of jakarta.transaction.SystemException in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
Transaction tx = mock(Transaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
given(tm.suspend()).willReturn(tx);
willThrow(new SystemException()).given(ut).begin();
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
}
}));
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
verify(tm).resume(tx);
}
use of jakarta.transaction.SystemException in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnIsExisting.
@Test
public void jtaTransactionManagerWithSystemExceptionOnIsExisting() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willThrow(new SystemException("system exception"));
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
});
}
Aggregations