Search in sources :

Example 21 with TransactionSynchronizationAdapter

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"));
}
Also used : User(com.thoughtworks.go.domain.User) NullUser(com.thoughtworks.go.domain.NullUser) TransactionStatus(org.springframework.transaction.TransactionStatus) Matchers.containsString(org.hamcrest.Matchers.containsString) SqlMapClientDaoSupport(com.thoughtworks.go.server.transaction.SqlMapClientDaoSupport) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 22 with TransactionSynchronizationAdapter

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;
}
Also used : MaterialConfigs(com.thoughtworks.go.config.materials.MaterialConfigs) JobInstance(com.thoughtworks.go.domain.JobInstance) MaterialRevisions(com.thoughtworks.go.domain.MaterialRevisions) StaleMaterialsOnBuildCause(com.thoughtworks.go.server.materials.StaleMaterialsOnBuildCause) MaterialConfig(com.thoughtworks.go.domain.materials.MaterialConfig) Material(com.thoughtworks.go.domain.materials.Material) ServerHealthState(com.thoughtworks.go.serverhealth.ServerHealthState) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) MaterialRevision(com.thoughtworks.go.domain.MaterialRevision) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Pipeline(com.thoughtworks.go.domain.Pipeline)

Example 23 with TransactionSynchronizationAdapter

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

Example 24 with TransactionSynchronizationAdapter

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

Example 25 with TransactionSynchronizationAdapter

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

Aggregations

TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)30 Test (org.junit.Test)14 TransactionStatus (org.springframework.transaction.TransactionStatus)14 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)13 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 SystemException (javax.transaction.SystemException)3 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)1 JobInstance (com.thoughtworks.go.domain.JobInstance)1 MaterialRevision (com.thoughtworks.go.domain.MaterialRevision)1 MaterialRevisions (com.thoughtworks.go.domain.MaterialRevisions)1 NullUser (com.thoughtworks.go.domain.NullUser)1 Pipeline (com.thoughtworks.go.domain.Pipeline)1 PipelineTimelineEntry (com.thoughtworks.go.domain.PipelineTimelineEntry)1 User (com.thoughtworks.go.domain.User)1 Material (com.thoughtworks.go.domain.materials.Material)1