use of org.springframework.transaction.jta.JtaTransactionManager 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);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}
});
fail("Should have thrown TransactionSystemException");
} catch (TransactionSystemException ex) {
// expected
}
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationSupportsAndSynchronizationOnActual.
@Test
public void jtaTransactionManagerWithPropagationSupportsAndSynchronizationOnActual() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
ptm.afterPropertiesSet();
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnCommit.
@Test
public void jtaTransactionManagerWithSystemExceptionOnCommit() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new SystemException("system exception")).given(ut).commit();
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
}
});
}
});
fail("Should have thrown TransactionSystemException");
} catch (TransactionSystemException ex) {
// expected
}
verify(ut).begin();
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackAndSynchronizationOnActual.
@Test
public void jtaTransactionManagerWithRollbackAndSynchronizationOnActual() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
tt.setTimeout(10);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setTransactionTimeout(10);
verify(ut).begin();
verify(ut).rollback();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
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();
}
Aggregations