Search in sources :

Example 6 with TransactionManager

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;
}
Also used : JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager)

Example 7 with TransactionManager

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);
}
Also used : TransactionManager(jakarta.transaction.TransactionManager) AbstractPlatformTransactionManager(org.springframework.transaction.support.AbstractPlatformTransactionManager)

Example 8 with TransactionManager

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();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) SimpleNamingContextBuilder(org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(jakarta.transaction.TransactionManager) Test(org.junit.jupiter.api.Test)

Example 9 with TransactionManager

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();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) SystemException(jakarta.transaction.SystemException) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 10 with TransactionManager

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);
}
Also used : JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(jakarta.transaction.Transaction) UserTransaction(jakarta.transaction.UserTransaction) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionManager (jakarta.transaction.TransactionManager)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 UserTransaction (jakarta.transaction.UserTransaction)10 Test (org.junit.jupiter.api.Test)9 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Transaction (jakarta.transaction.Transaction)5 SystemException (jakarta.transaction.SystemException)2 ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)2 SimpleNamingContextBuilder (org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder)1 UserTransactionAdapter (org.springframework.transaction.jta.UserTransactionAdapter)1 AbstractPlatformTransactionManager (org.springframework.transaction.support.AbstractPlatformTransactionManager)1 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)1