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"));
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class ScheduledPipelineLoader method pipelineWithPasswordAwareBuildCauseByBuildId.
//TODO: Do we need to do this differently than PipelineService#fullPipeline?
public Pipeline pipelineWithPasswordAwareBuildCauseByBuildId(final long buildId) {
Pipeline pipeline = pipelineDao.pipelineWithMaterialsAndModsByBuildId(buildId);
MaterialRevisions scheduledRevs = pipeline.getBuildCause().getMaterialRevisions();
MaterialConfigs knownMaterials = knownMaterials(pipeline, scheduledRevs);
for (MaterialRevision materialRevision : scheduledRevs) {
MaterialConfig materialConfig = materialFrom(knownMaterials, materialRevision);
Material usedMaterial = materialRevision.getMaterial();
if (materialConfig == null) {
final JobInstance jobInstance = jobInstanceService.buildByIdWithTransitions(buildId);
scheduleService.failJob(jobInstance);
final String message = "Cannot load job '" + jobInstance.buildLocator() + "' because material " + usedMaterial.config() + " was not found in config.";
final String description = "Job for pipeline '" + jobInstance.buildLocator() + "' has been failed as one or more material configurations were either changed or removed.";
transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
final ServerHealthState error = ServerHealthState.error(message, description, HealthStateType.general(HealthStateScope.forJob(jobInstance.getPipelineName(), jobInstance.getStageName(), jobInstance.getName())));
error.setTimeout(Timeout.FIVE_MINUTES);
serverHealthService.update(error);
appendToConsoleLog(jobInstance, message);
appendToConsoleLog(jobInstance, description);
}
});
throw new StaleMaterialsOnBuildCause(message);
}
usedMaterial.updateFromConfig(materialConfig);
}
return pipeline;
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class StageService method updateStageWithoutNotifications.
private void updateStageWithoutNotifications(final Stage stage) {
stage.calculateResult();
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
clearCachedCompletedStageFeeds(stage.getIdentifier().getPipelineName());
}
});
stageDao.updateResult(stage, stage.getResult());
}
});
}
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));
}
Aggregations