use of jakarta.transaction.Transaction 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.Transaction 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.Transaction 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.Transaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNew.
@Test
public void jtaTransactionManagerWithPropagationRequiresNew() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
Transaction tx = mock(Transaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
given(tm.suspend()).willReturn(tx);
final JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
tt.setName("txName");
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName");
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
TransactionTemplate tt2 = new TransactionTemplate(ptm);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
tt2.setReadOnly(true);
tt2.setName("txName2");
tt2.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName2");
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isTrue();
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName");
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
verify(ut, times(2)).begin();
verify(ut, times(2)).commit();
verify(tm).resume(tx);
}
use of jakarta.transaction.Transaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExisting.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() 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);
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
verify(ut).begin();
verify(ut).commit();
verify(tm).resume(tx);
}
Aggregations