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());
}
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();
}
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();
}
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();
}
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();
}
Aggregations