Search in sources :

Example 11 with TransactionSynchronizationAdapter

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;
}
Also used : TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) File(java.io.File) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) GitCommand(com.thoughtworks.go.domain.materials.git.GitCommand)

Example 12 with TransactionSynchronizationAdapter

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()));
}
Also used : TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) Pipeline(com.thoughtworks.go.domain.Pipeline) PipelineState(com.thoughtworks.go.domain.PipelineState) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 13 with TransactionSynchronizationAdapter

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()));
        }
    });
}
Also used : StageStatusMessage(com.thoughtworks.go.server.messaging.StageStatusMessage) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 14 with TransactionSynchronizationAdapter

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));
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 15 with TransactionSynchronizationAdapter

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));
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Aggregations

TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)34 Test (org.junit.Test)16 TransactionStatus (org.springframework.transaction.TransactionStatus)15 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)14 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 UserTransaction (javax.transaction.UserTransaction)7 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)7 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)6 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5 Pipeline (com.thoughtworks.go.domain.Pipeline)3 SystemException (javax.transaction.SystemException)3 PipelineState (com.thoughtworks.go.domain.PipelineState)2 GitCommand (com.thoughtworks.go.domain.materials.git.GitCommand)2 TransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TransactionSynchronizationManager)2 File (java.io.File)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 SimpleTransactionStatus (org.springframework.transaction.support.SimpleTransactionStatus)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2