use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class TransactionTemplateTest method shouldAllowRegistrationOfTransactionSynchronization_inTransactionSurroundingBlock_andNotExecuteSynchronizationIfTransactionNeverHappens.
@Test
public void shouldAllowRegistrationOfTransactionSynchronization_inTransactionSurroundingBlock_andNotExecuteSynchronizationIfTransactionNeverHappens() {
TransactionTemplate template = new TransactionTemplate(transactionTemplate);
final boolean[] afterCommitHappened = new boolean[1];
String returnVal = (String) template.transactionSurrounding(new TransactionTemplate.TransactionSurrounding<RuntimeException>() {
public Object surrounding() {
transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
afterCommitHappened[0] = true;
}
});
return "bar";
}
});
assertThat(returnVal, is("bar"));
assertThat(afterCommitHappened[0], is(false));
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class TransactionTemplateTest method shouldAllowRegistrationOfTransactionSynchronization_inTransactionSurroundingBlock_andExecuteAppropriateHooks.
@Test
public void shouldAllowRegistrationOfTransactionSynchronization_inTransactionSurroundingBlock_andExecuteAppropriateHooks() {
final TransactionTemplate template = new TransactionTemplate(transactionTemplate);
final boolean[] afterCommitHappened = new boolean[1];
final boolean[] transactionWasActiveInSurrounding = new boolean[1];
final boolean[] transactionWasActiveInTransaction = new boolean[1];
String returnVal = (String) template.transactionSurrounding(new TransactionTemplate.TransactionSurrounding<RuntimeException>() {
public Object surrounding() {
transactionWasActiveInSurrounding[0] = transactionSynchronizationManager.isTransactionBodyExecuting();
transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
afterCommitHappened[0] = true;
}
});
return template.execute(new org.springframework.transaction.support.TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
transactionWasActiveInTransaction[0] = transactionSynchronizationManager.isTransactionBodyExecuting();
return "foo";
}
});
}
});
assertThat(returnVal, is("foo"));
assertThat(afterCommitHappened[0], is(true));
assertThat(transactionWasActiveInSurrounding[0], is(false));
assertThat(transactionWasActiveInTransaction[0], is(true));
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class TransactionTemplateTest method shouldPropagateExceptionsOutOfTransactionSurrounding.
@Test
public void shouldPropagateExceptionsOutOfTransactionSurrounding() throws IOException {
TransactionTemplate template = new TransactionTemplate(transactionTemplate);
final boolean[] afterCommitHappened = new boolean[1];
String returnVal = null;
try {
returnVal = (String) template.transactionSurrounding(new TransactionTemplate.TransactionSurrounding<IOException>() {
public Object surrounding() throws IOException {
transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
afterCommitHappened[0] = true;
}
});
throw new IOException("boo ha!");
}
});
fail("should have propagated exception");
} catch (IOException e) {
assertThat(e.getMessage(), is("boo ha!"));
}
assertThat(returnVal, nullValue());
assertThat(afterCommitHappened[0], is(false));
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class GoCacheTest method shouldStartServingThingsOutOfCacheOnceTransactionCompletes.
@Test
public void shouldStartServingThingsOutOfCacheOnceTransactionCompletes() {
final SqlMapClientDaoSupport daoSupport = new SqlMapClientDaoSupport(goCache, sqlMapClient, systemEnvironment, databaseStrategy);
goCache.put("foo", "bar");
final String[] valueInCleanTxn = new String[1];
final String[] valueInDirtyTxn = new String[1];
final String[] valueInAfterCommit = new String[1];
final String[] valueInAfterCompletion = new String[1];
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
valueInCleanTxn[0] = (String) goCache.get("foo");
User user = new User("loser", "Massive Loser", "boozer@loser.com");
userSqlMapDao.saveOrUpdate(user);
valueInDirtyTxn[0] = (String) goCache.get("foo");
transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
valueInAfterCommit[0] = (String) goCache.get("foo");
}
@Override
public void afterCompletion(int status) {
valueInAfterCompletion[0] = (String) goCache.get("foo");
}
});
}
});
assertThat(valueInCleanTxn[0], is("bar"));
assertThat(valueInDirtyTxn[0], is(nullValue()));
assertThat(valueInAfterCommit[0], is("bar"));
assertThat(valueInAfterCompletion[0], is("bar"));
}
Aggregations