use of org.springframework.transaction.jta.JtaTransactionObject in project spring-framework by spring-projects.
the class DataSourceJtaTransactionTests method testJtaTransactionWithConnectionHolderStillBound.
@Test
public void testJtaTransactionWithConnectionHolderStillBound() throws Exception {
@SuppressWarnings("serial") JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) {
@Override
protected void doRegisterAfterCompletionWithJtaTransaction(JtaTransactionObject txObject, final List<TransactionSynchronization> synchronizations) throws RollbackException, SystemException {
Thread async = new Thread() {
@Override
public void run() {
invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED);
}
};
async.start();
try {
async.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
};
TransactionTemplate tt = new TransactionTemplate(ptm);
boolean condition2 = !TransactionSynchronizationManager.hasResource(dataSource);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("JTA synchronizations not active").isTrue();
given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE);
for (int i = 0; i < 3; i++) {
final boolean releaseCon = (i != 1);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("JTA synchronizations active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is existing transaction").isTrue();
Connection c = DataSourceUtils.getConnection(dataSource);
assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Has thread connection").isTrue();
DataSourceUtils.releaseConnection(c, dataSource);
c = DataSourceUtils.getConnection(dataSource);
assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Has thread connection").isTrue();
if (releaseCon) {
DataSourceUtils.releaseConnection(c, dataSource);
}
}
});
if (!releaseCon) {
assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Still has connection holder").isTrue();
} else {
boolean condition = !TransactionSynchronizationManager.hasResource(dataSource);
assertThat(condition).as("Hasn't thread connection").isTrue();
}
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).as("JTA synchronizations not active").isTrue();
}
verify(connection, times(3)).close();
}
Aggregations