use of jakarta.transaction.Transaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationNotSupported.
@Test
public void jtaTransactionManagerWithPropagationNotSupported() 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_NOT_SUPPORTED);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
status.setRollbackOnly();
}
});
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
verify(tm).resume(tx);
}
use of jakarta.transaction.Transaction 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.Transaction 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.Transaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndAdapter.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception {
TransactionManager tm = mock(TransactionManager.class);
Transaction tx = mock(Transaction.class);
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
given(tm.suspend()).willReturn(tx);
JtaTransactionManager ptm = newJtaTransactionManager(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(tm).begin();
verify(tm).commit();
verify(tm).resume(tx);
}
Aggregations