Search in sources :

Example 26 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager 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 = (JtaTransactionManager) SerializationTestUtils.serializeAndDeserialize(jtam);
    // should do client-side lookup
    assertNotNull("Logger must survive serialization", serializedJtatm.logger);
    assertTrue("UserTransaction looked up on client", serializedJtatm.getUserTransaction() == ut2);
    assertNull("TransactionManager didn't survive", serializedJtatm.getTransactionManager());
    assertEquals(true, serializedJtatm.isRollbackOnCommitFailure());
}
Also used : UserTransaction(javax.transaction.UserTransaction) SimpleNamingContextBuilder(org.springframework.tests.mock.jndi.SimpleNamingContextBuilder) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) Test(org.junit.Test)

Example 27 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager in project spring-boot by spring-projects.

the class JtaAutoConfigurationTests method bitronixCustomizeJtaTransactionManagerUsingProperties.

@Test
public void bitronixCustomizeJtaTransactionManagerUsingProperties() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.transaction.default-timeout:30", "spring.transaction.rollback-on-commit-failure:true");
    this.context.register(BitronixJtaConfiguration.class, TransactionAutoConfiguration.class);
    this.context.refresh();
    JtaTransactionManager transactionManager = this.context.getBean(JtaTransactionManager.class);
    assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
    assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Test(org.junit.Test)

Example 28 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager in project spring-boot by spring-projects.

the class JtaAutoConfigurationTests method atomikosCustomizeJtaTransactionManagerUsingProperties.

@Test
public void atomikosCustomizeJtaTransactionManagerUsingProperties() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.transaction.default-timeout:30", "spring.transaction.rollback-on-commit-failure:true");
    this.context.register(AtomikosJtaConfiguration.class, TransactionAutoConfiguration.class);
    this.context.refresh();
    JtaTransactionManager transactionManager = this.context.getBean(JtaTransactionManager.class);
    assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
    assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Test(org.junit.Test)

Example 29 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.

the class DataSourceJtaTransactionTests method doTestJtaTransactionWithIsolationLevelDataSourceRouter.

private void doTestJtaTransactionWithIsolationLevelDataSourceRouter(boolean dataSourceLookup) throws Exception {
    given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    final DataSource dataSource1 = mock(DataSource.class);
    final Connection connection1 = mock(Connection.class);
    given(dataSource1.getConnection()).willReturn(connection1);
    final DataSource dataSource2 = mock(DataSource.class);
    final Connection connection2 = mock(Connection.class);
    given(dataSource2.getConnection()).willReturn(connection2);
    final IsolationLevelDataSourceRouter dsToUse = new IsolationLevelDataSourceRouter();
    Map<Object, Object> targetDataSources = new HashMap<>();
    if (dataSourceLookup) {
        targetDataSources.put("ISOLATION_REPEATABLE_READ", "ds2");
        dsToUse.setDefaultTargetDataSource("ds1");
        StaticListableBeanFactory beanFactory = new StaticListableBeanFactory();
        beanFactory.addBean("ds1", dataSource1);
        beanFactory.addBean("ds2", dataSource2);
        dsToUse.setDataSourceLookup(new BeanFactoryDataSourceLookup(beanFactory));
    } else {
        targetDataSources.put("ISOLATION_REPEATABLE_READ", dataSource2);
        dsToUse.setDefaultTargetDataSource(dataSource1);
    }
    dsToUse.setTargetDataSources(targetDataSources);
    dsToUse.afterPropertiesSet();
    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction);
    ptm.setAllowCustomIsolationLevels(true);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Connection c = DataSourceUtils.getConnection(dsToUse);
            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
            assertSame(connection1, c);
            DataSourceUtils.releaseConnection(c, dsToUse);
        }
    });
    tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Connection c = DataSourceUtils.getConnection(dsToUse);
            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
            assertSame(connection2, c);
            DataSourceUtils.releaseConnection(c, dsToUse);
        }
    });
    verify(userTransaction, times(2)).begin();
    verify(userTransaction, times(2)).commit();
    verify(connection1).close();
    verify(connection2).close();
}
Also used : StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory) HashMap(java.util.HashMap) Connection(java.sql.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) IsolationLevelDataSourceRouter(org.springframework.jdbc.datasource.lookup.IsolationLevelDataSourceRouter) TransactionStatus(org.springframework.transaction.TransactionStatus) BeanFactoryDataSourceLookup(org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup) DataSource(javax.sql.DataSource) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionObject(org.springframework.transaction.jta.JtaTransactionObject) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 30 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.

the class DataSourceJtaTransactionTests method doTestJtaTransaction.

private void doTestJtaTransaction(final boolean rollback) throws Exception {
    if (rollback) {
        given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    } else {
        given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    }
    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
            assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
            assertTrue("Is new transaction", status.isNewTransaction());
            Connection c = DataSourceUtils.getConnection(dataSource);
            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
            DataSourceUtils.releaseConnection(c, dataSource);
            c = DataSourceUtils.getConnection(dataSource);
            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource));
            DataSourceUtils.releaseConnection(c, dataSource);
            if (rollback) {
                status.setRollbackOnly();
            }
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
    verify(userTransaction).begin();
    if (rollback) {
        verify(userTransaction).rollback();
    }
    verify(connection).close();
}
Also used : JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)57 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)50 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)50 Test (org.junit.Test)49 UserTransaction (javax.transaction.UserTransaction)47 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)13 TransactionManager (javax.transaction.TransactionManager)11 SystemException (javax.transaction.SystemException)8 Connection (java.sql.Connection)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)7 MockJtaTransaction (org.springframework.tests.transaction.MockJtaTransaction)6 Transaction (javax.transaction.Transaction)5 DataSource (javax.sql.DataSource)4 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)4 ExpectedLookupTemplate (org.springframework.tests.mock.jndi.ExpectedLookupTemplate)3 SQLException (java.sql.SQLException)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 JtaTransactionObject (org.springframework.transaction.jta.JtaTransactionObject)2