use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class GitMaterial method git.
private GitCommand git(ConsoleOutputStreamConsumer outputStreamConsumer, final File workingFolder, int preferredCloneDepth, SubprocessExecutionContext executionContext) throws Exception {
if (isSubmoduleFolder()) {
return new GitCommand(getFingerprint(), new File(workingFolder.getPath()), GitMaterialConfig.DEFAULT_BRANCH, true, executionContext.getDefaultEnvironmentVariables(), secrets());
}
GitCommand gitCommand = new GitCommand(getFingerprint(), workingFolder, getBranch(), false, executionContext.getDefaultEnvironmentVariables(), secrets());
if (!isGitRepository(workingFolder) || isRepositoryChanged(gitCommand, workingFolder)) {
LOG.debug("Invalid git working copy or repository changed. Delete folder: {}", workingFolder);
deleteDirectoryNoisily(workingFolder);
}
createParentFolderIfNotExist(workingFolder);
if (!workingFolder.exists()) {
TransactionSynchronizationManager txManager = new TransactionSynchronizationManager();
if (txManager.isActualTransactionActive()) {
txManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
if (status != TransactionSynchronization.STATUS_COMMITTED) {
FileUtils.deleteQuietly(workingFolder);
}
}
});
}
int cloneDepth = shallowClone ? preferredCloneDepth : Integer.MAX_VALUE;
int returnValue;
if (executionContext.isServer()) {
returnValue = gitCommand.cloneWithNoCheckout(outputStreamConsumer, url.forCommandline());
} else {
returnValue = gitCommand.clone(outputStreamConsumer, url.forCommandline(), cloneDepth);
}
bombIfFailedToRunCommandLine(returnValue, "Failed to run git clone command");
}
return gitCommand;
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class PipelineStateDaoCachingTest method unlockPipeline_shouldSavePipelineStateAndInvalidateCache.
@Test
public void unlockPipeline_shouldSavePipelineStateAndInvalidateCache() throws Exception {
final TransactionSynchronizationAdapter[] transactionSynchronizationAdapter = { null };
when(transactionTemplate.execute(any(org.springframework.transaction.support.TransactionCallbackWithoutResult.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
org.springframework.transaction.support.TransactionCallbackWithoutResult callback = (org.springframework.transaction.support.TransactionCallbackWithoutResult) invocation.getArguments()[0];
callback.doInTransaction(new SimpleTransactionStatus());
transactionSynchronizationAdapter[0].afterCommit();
return null;
}
});
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
TransactionSynchronizationAdapter adapter = (TransactionSynchronizationAdapter) invocation.getArguments()[0];
transactionSynchronizationAdapter[0] = adapter;
return null;
}
}).when(transactionSynchronizationManager).registerSynchronization(any(TransactionSynchronization.class));
final Pipeline pipeline = PipelineMother.pipeline("mingle");
PipelineState pipelineState = new PipelineState(pipeline.getName(), pipeline.getFirstStage().getIdentifier());
goCache.put(pipelineStateDao.pipelineLockStateCacheKey(pipeline.getName()), pipelineState);
when(session.load(PipelineState.class, pipeline.getId())).thenReturn(pipelineState);
pipelineStateDao.unlockPipeline(pipeline.getName());
assertThat(goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipeline.getName())), is(nullValue()));
ArgumentCaptor<PipelineState> pipelineStateArgumentCaptor = ArgumentCaptor.forClass(PipelineState.class);
verify(session).saveOrUpdate(pipelineStateArgumentCaptor.capture());
PipelineState savedPipelineState = pipelineStateArgumentCaptor.getValue();
assertThat(savedPipelineState.isLocked(), is(false));
assertThat(savedPipelineState.getLockedBy(), is(nullValue()));
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class StageService method cancelStage.
public synchronized void cancelStage(final Stage stage) {
cancel(stage);
notifyStageStatusChangeListeners(stage);
transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
stageStatusTopic.post(new StageStatusMessage(stage.getIdentifier(), stage.stageState(), stage.getResult(), UserHelper.getUserName()));
}
});
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class TransactionTemplateTest method should_NOT_useSynchronizationsFromOneSurroundingBlockInAnother.
@Test
public void should_NOT_useSynchronizationsFromOneSurroundingBlockInAnother() {
final 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 "foo";
}
});
assertThat(returnVal, is("foo"));
// because no transaction happened
assertThat(afterCommitHappened[0], is(false));
returnVal = (String) template.transactionSurrounding(new TransactionTemplate.TransactionSurrounding<RuntimeException>() {
public Object surrounding() {
return template.execute(new org.springframework.transaction.support.TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return "bar";
}
});
}
});
assertThat(returnVal, is("bar"));
// because it registered no synchronization
assertThat(afterCommitHappened[0], is(false));
}
use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.
the class TransactionTemplateTest method shouldNotRegisterSynchronizationMultipleTimes.
@Test
public void shouldNotRegisterSynchronizationMultipleTimes() {
final TransactionTemplate template = new TransactionTemplate(transactionTemplate);
final int[] numberOfTimesAfterCommitHappened = new int[1];
numberOfTimesAfterCommitHappened[0] = 0;
String returnVal = (String) template.transactionSurrounding(new TransactionTemplate.TransactionSurrounding<RuntimeException>() {
public Object surrounding() {
transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
numberOfTimesAfterCommitHappened[0]++;
}
});
return template.execute(new org.springframework.transaction.support.TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return goTransactionTemplate.execute(new org.springframework.transaction.support.TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return "foo";
}
});
}
});
}
});
assertThat(returnVal, is("foo"));
assertThat(numberOfTimesAfterCommitHappened[0], is(1));
}
Aggregations