Search in sources :

Example 16 with TransactionSynchronizationAdapter

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

the class MaterialRepository method save.

private void save(final PipelineMaterialRevision pipelineMaterialRevision, final String pipelineName) {
    getHibernateTemplate().save(pipelineMaterialRevision);
    transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

        @Override
        public void afterCommit() {
            String key = cacheKeyForLatestPmrForPipelineKey(pipelineMaterialRevision.getMaterialId(), pipelineName.toLowerCase());
            synchronized (key) {
                goCache.remove(key);
            }
        }
    });
}
Also used : CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 17 with TransactionSynchronizationAdapter

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

the class GoCache method stopServingForTransaction.

public void stopServingForTransaction() {
    if (transactionSynchronizationManager.isTransactionBodyExecuting() && !doNotServeForTransaction()) {
        doNotServeForTransaction.set(true);
        transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

            @Override
            public void beforeCompletion() {
                doNotServeForTransaction.set(false);
            }
        });
    }
}
Also used : TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 18 with TransactionSynchronizationAdapter

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

the class PipelineSqlMapDao method unlockPipeline.

public void unlockPipeline(final String pipelineName) {
    lockPipelineMutex.writeLock().lock();
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            final String cacheKey = lockedPipelineCacheKey(pipelineName);
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCompletion(int status) {
                    goCache.remove(cacheKey);
                    lockPipelineMutex.writeLock().unlock();
                }
            });
            getSqlMapClientTemplate().update("unlockLockedPipeline", pipelineName);
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 19 with TransactionSynchronizationAdapter

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

the class PipelineSqlMapDao method lockPipeline.

public void lockPipeline(final Pipeline pipeline) {
    lockPipelineMutex.writeLock().lock();
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            final String pipelineName = pipeline.getName();
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCompletion(int status) {
                    clearLockedPipelineCache(pipelineName);
                    lockPipelineMutex.writeLock().unlock();
                }
            });
            StageIdentifier identifier = lockedPipeline(pipelineName);
            if (identifier != null && !identifier.pipelineIdentifier().equals(pipeline.getIdentifier())) {
                throw new RuntimeException(String.format("Pipeline '%s' is already locked (counter = %s)", pipelineName, identifier.getPipelineCounter()));
            }
            getSqlMapClientTemplate().update("lockPipeline", pipeline.getId());
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 20 with TransactionSynchronizationAdapter

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

the class PipelineSqlMapDao method save.

public Pipeline save(final Pipeline pipeline) {
    return (Pipeline) transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    goCache.remove(cacheKeyForLatestPipelineIdByPipelineName(pipeline.getName()));
                    invalidateCacheConditionallyForPipelineInstancesTriggeredWithDependencyMaterial(pipeline);
                }
            });
            Long pipelineId = (Long) getSqlMapClientTemplate().insert("insertPipeline", pipeline);
            savePipelineMaterialRevisions(pipeline, pipelineId);
            environmentVariableDao.save(pipelineId, EnvironmentVariableSqlMapDao.EnvironmentVariableType.Trigger, pipeline.scheduleTimeVariables());
            return pipeline;
        }
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

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