Search in sources :

Example 76 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project gocd by gocd.

the class AccessTokenSqlMapDao method findAllTokensForUser.

@Override
public List<AccessToken> findAllTokensForUser(String username, AccessTokenFilter filter) {
    return (List<AccessToken>) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(AccessToken.class).add(Restrictions.eq("username", username)).add(Restrictions.eq("deletedBecauseUserDeleted", false));
        filter.applyTo(criteria);
        return criteria.setCacheable(true).list();
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) List(java.util.List) Criteria(org.hibernate.Criteria)

Example 77 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project gocd by gocd.

the class PluggableSCMMaterialUpdater method insertLatestOrNewModifications.

@Override
public void insertLatestOrNewModifications(final Material material, MaterialInstance materialInstance, File folder, Modifications list) {
    final PluggableSCMMaterialInstance currentMaterialInstance = (PluggableSCMMaterialInstance) materialInstance;
    final PluggableSCMMaterialInstance latestMaterialInstance = (PluggableSCMMaterialInstance) material.createMaterialInstance();
    if (currentMaterialInstance.shouldUpgradeTo(latestMaterialInstance)) {
        transactionTemplate.execute((TransactionCallback) transactionStatus -> {
            PluggableSCMMaterialInstance materialInstance1 = (PluggableSCMMaterialInstance) materialRepository.find(currentMaterialInstance.getId());
            materialInstance1.upgradeTo(latestMaterialInstance);
            materialRepository.saveOrUpdate(materialInstance1);
            return materialInstance1;
        });
    }
    scmMaterialUpdater.insertLatestOrNewModifications(material, currentMaterialInstance, folder, list);
}
Also used : MaterialRepository(com.thoughtworks.go.server.persistence.MaterialRepository) Component(org.springframework.stereotype.Component) Material(com.thoughtworks.go.domain.materials.Material) Autowired(org.springframework.beans.factory.annotation.Autowired) TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionTemplate(com.thoughtworks.go.server.transaction.TransactionTemplate) Modifications(com.thoughtworks.go.domain.materials.Modifications) PluggableSCMMaterialInstance(com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance) File(java.io.File) MaterialInstance(com.thoughtworks.go.domain.MaterialInstance) PluggableSCMMaterialInstance(com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance)

Example 78 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project gocd by gocd.

the class PackageMaterialUpdater method insertLatestOrNewModifications.

@Override
public void insertLatestOrNewModifications(final Material material, MaterialInstance materialInstance, File folder, Modifications list) {
    final PackageMaterialInstance packageMaterialInstance = (PackageMaterialInstance) materialInstance;
    if (packageMaterialInstance.shouldUpgradeTo((PackageMaterialInstance) material.createMaterialInstance())) {
        transactionTemplate.execute((TransactionCallback) transactionStatus -> {
            PackageMaterialInstance materialInstance1 = (PackageMaterialInstance) materialRepository.find(packageMaterialInstance.getId());
            materialInstance1.upgradeTo((PackageMaterialInstance) material.createMaterialInstance());
            materialRepository.saveOrUpdate(materialInstance1);
            return materialInstance1;
        });
    }
    scmMaterialUpdater.insertLatestOrNewModifications(material, packageMaterialInstance, folder, list);
}
Also used : MaterialRepository(com.thoughtworks.go.server.persistence.MaterialRepository) Component(org.springframework.stereotype.Component) Material(com.thoughtworks.go.domain.materials.Material) Autowired(org.springframework.beans.factory.annotation.Autowired) TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionTemplate(com.thoughtworks.go.server.transaction.TransactionTemplate) Modifications(com.thoughtworks.go.domain.materials.Modifications) File(java.io.File) MaterialInstance(com.thoughtworks.go.domain.MaterialInstance) PackageMaterialInstance(com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance) PackageMaterialInstance(com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance)

Example 79 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project gocd by gocd.

the class PipelineSqlMapDaoCachingTest method shouldInvalidateCacheOfPipelineInstancesTriggeredWithMaterialRevision.

@Test
void shouldInvalidateCacheOfPipelineInstancesTriggeredWithMaterialRevision() {
    GitMaterialInstance materialInstance = new GitMaterialInstance("url", null, "branch", "submodule", "flyweight");
    String cacheKey = pipelineDao.cacheKeyForPipelineInstancesTriggeredWithDependencyMaterial("p1", materialInstance.getFingerprint(), "r1");
    List<PipelineIdentifier> result = Arrays.asList(new PipelineIdentifier("p1", 1, "1"));
    when(mockTemplate.queryForList(eq("pipelineInstancesTriggeredOffOfMaterialRevision"), any())).thenReturn((List) result);
    List<PipelineIdentifier> actual = pipelineDao.getPipelineInstancesTriggeredWithDependencyMaterial("p1", materialInstance, "r1");
    assertThat(actual).hasSize(1);
    assertThat((List<PipelineIdentifier>) goCache.get(cacheKey)).hasSize(1);
    MaterialRevisions materialRevisions = new MaterialRevisions(new MaterialRevision(new GitMaterial("url", "branch"), new Modification("user", "comment", "email", new Date(), "r1")));
    Pipeline pipeline = new Pipeline("p1", BuildCause.createWithModifications(materialRevisions, ""));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            ((TransactionSynchronizationAdapter) invocation.getArguments()[0]).afterCommit();
            return null;
        }
    }).when(transactionSynchronizationManager).registerSynchronization(any(TransactionSynchronization.class));
    when(transactionTemplate.execute(any(TransactionCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            ((TransactionCallback) invocation.getArguments()[0]).doInTransaction(new SimpleTransactionStatus());
            return null;
        }
    });
    pipelineDao.save(pipeline);
    assertThat(goCache.get(cacheKey)).isNull();
}
Also used : GitMaterialInstance(com.thoughtworks.go.domain.materials.git.GitMaterialInstance) Modification(com.thoughtworks.go.domain.materials.Modification) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionCallback(org.springframework.transaction.support.TransactionCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) Test(org.junit.jupiter.api.Test)

Example 80 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project gocd by gocd.

the class StageSqlMapDao method save.

@Override
public Stage save(final Pipeline pipeline, final Stage stage) {
    return (Stage) transactionTemplate.execute((TransactionCallback) status -> {
        transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

            @Override
            public void afterCommit() {
                String pipelineName = pipeline.getName();
                String stageName = stage.getName();
                clearStageHistoryPageCaches(stage, pipelineName, false);
                clearCachedStage(stage.getIdentifier());
                clearCachedAllStages(pipelineName, pipeline.getCounter(), stageName);
                removeFromCache(cacheKeyForStageCountForGraph(pipelineName, stageName));
            }
        });
        stage.setPipelineId(pipeline.getId());
        int maxStageCounter = getMaxStageCounter(pipeline.getId(), stage.getName());
        stage.setCounter(maxStageCounter + 1);
        getSqlMapClientTemplate().update("markPreviousStageRunsAsNotLatest", arguments("stageName", stage.getName()).and("pipelineId", pipeline.getId()).asMap());
        getSqlMapClientTemplate().insert("insertStage", stage);
        stage.setIdentifier(new StageIdentifier(pipeline, stage));
        return stage;
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Aggregations

TransactionCallback (org.springframework.transaction.support.TransactionCallback)100 TransactionStatus (org.springframework.transaction.TransactionStatus)75 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)14 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)10 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)9 PreparedStatement (java.sql.PreparedStatement)8 List (java.util.List)8 GitMaterial (com.thoughtworks.go.config.materials.git.GitMaterial)7 MaterialInstance (com.thoughtworks.go.domain.MaterialInstance)7 Modification (com.thoughtworks.go.domain.materials.Modification)7 PackageMaterialInstance (com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance)7 PluggableSCMMaterialInstance (com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance)7 Query (org.hibernate.Query)7 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 Modifications (com.thoughtworks.go.domain.materials.Modifications)6 HgMaterialInstance (com.thoughtworks.go.domain.materials.mercurial.HgMaterialInstance)6