use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.
the class LocalSessionFactoryBuilder method setJtaTransactionManager.
/**
* Set the Spring {@link JtaTransactionManager} or the JTA {@link TransactionManager}
* to be used with Hibernate, if any. Allows for using a Spring-managed transaction
* manager for Hibernate 5's session and cache synchronization, with the
* "hibernate.transaction.jta.platform" automatically set to it.
* <p>A passed-in Spring {@link JtaTransactionManager} needs to contain a JTA
* {@link TransactionManager} reference to be usable here, except for the WebSphere
* case where we'll automatically set {@code WebSphereExtendedJtaPlatform} accordingly.
* <p>Note: If this is set, the Hibernate settings should not contain a JTA platform
* setting to avoid meaningless double configuration.
*/
public LocalSessionFactoryBuilder setJtaTransactionManager(Object jtaTransactionManager) {
Assert.notNull(jtaTransactionManager, "Transaction manager reference must not be null");
if (jtaTransactionManager instanceof JtaTransactionManager) {
boolean webspherePresent = ClassUtils.isPresent("com.ibm.wsspi.uow.UOWManager", getClass().getClassLoader());
if (webspherePresent) {
getProperties().put(AvailableSettings.JTA_PLATFORM, "org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform");
} else {
JtaTransactionManager jtaTm = (JtaTransactionManager) jtaTransactionManager;
if (jtaTm.getTransactionManager() == null) {
throw new IllegalArgumentException("Can only apply JtaTransactionManager which has a TransactionManager reference set");
}
getProperties().put(AvailableSettings.JTA_PLATFORM, new ConfigurableJtaPlatform(jtaTm.getTransactionManager(), jtaTm.getUserTransaction(), jtaTm.getTransactionSynchronizationRegistry()));
}
} else if (jtaTransactionManager instanceof TransactionManager) {
getProperties().put(AvailableSettings.JTA_PLATFORM, new ConfigurableJtaPlatform((TransactionManager) jtaTransactionManager, null, null));
} else {
throw new IllegalArgumentException("Unknown transaction manager type: " + jtaTransactionManager.getClass().getName());
}
getProperties().put(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta");
getProperties().put(AvailableSettings.CONNECTION_HANDLING, PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT);
return this;
}
use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManager method createTransaction.
// ---------------------------------------------------------------------
// Implementation of TransactionFactory interface
// ---------------------------------------------------------------------
@Override
public Transaction createTransaction(@Nullable String name, int timeout) throws NotSupportedException, SystemException {
TransactionManager tm = getTransactionManager();
Assert.state(tm != null, "No JTA TransactionManager available");
if (timeout >= 0) {
tm.setTransactionTimeout(timeout);
}
tm.begin();
return new ManagedTransactionAdapter(tm);
}
use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerSerializationTests method serializable.
@Test
public void serializable() throws Exception {
UserTransaction ut1 = mock(UserTransaction.class);
UserTransaction ut2 = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
JtaTransactionManager jtam = new JtaTransactionManager();
jtam.setUserTransaction(ut1);
jtam.setTransactionManager(tm);
jtam.setRollbackOnCommitFailure(true);
jtam.afterPropertiesSet();
SimpleNamingContextBuilder jndiEnv = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
jndiEnv.bind(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut2);
JtaTransactionManager serializedJtatm = SerializationTestUtils.serializeAndDeserialize(jtam);
// should do client-side lookup
assertThat(serializedJtatm.logger).as("Logger must survive serialization").isNotNull();
assertThat(serializedJtatm.getUserTransaction() == ut2).as("UserTransaction looked up on client").isTrue();
assertThat(serializedJtatm.getTransactionManager()).as("TransactionManager didn't survive").isNull();
assertThat(serializedJtatm.isRollbackOnCommitFailure()).isTrue();
}
use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new SystemException()).given(tm).suspend();
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
}
}));
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
}
use of jakarta.transaction.TransactionManager 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