Search in sources :

Example 31 with TransactionCallback

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

the class PipelineRepositoryIntegrationTest method save.

private long save(final PipelineConfig pipelineConfig, final int counter, final double naturalOrder, final MaterialRevision... materialRevisions) {
    return (Long) transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            MaterialRevisions revisions = new MaterialRevisions(materialRevisions);
            materialRepository.save(revisions);
            Pipeline instance = instanceFactory.createPipelineInstance(pipelineConfig, BuildCause.createWithModifications(revisions, "me"), new DefaultSchedulingContext(), "md5-test", new TimeProvider());
            instance.setCounter(counter);
            instance.setNaturalOrder(naturalOrder);
            return pipelineSqlMapDao.save(instance).getId();
        }
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TimeProvider(com.thoughtworks.go.util.TimeProvider) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 32 with TransactionCallback

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

the class PipelineStateDao method pipelineStateFor.

public PipelineState pipelineStateFor(String pipelineName) {
    String cacheKey = pipelineLockStateCacheKey(pipelineName);
    PipelineState pipelineState = (PipelineState) goCache.get(cacheKey);
    if (pipelineState != null) {
        return pipelineState.equals(PipelineState.NOT_LOCKED) ? null : pipelineState;
    }
    synchronized (cacheKey) {
        pipelineState = (PipelineState) goCache.get(cacheKey);
        if (pipelineState != null) {
            return pipelineState.equals(PipelineState.NOT_LOCKED) ? null : pipelineState;
        }
        pipelineState = (PipelineState) transactionTemplate.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus transactionStatus) {
                return sessionFactory.getCurrentSession().createCriteria(PipelineState.class).add(Restrictions.eq("pipelineName", pipelineName)).setCacheable(false).uniqueResult();
            }
        });
        if (pipelineState != null && pipelineState.isLocked()) {
            StageIdentifier lockedBy = (StageIdentifier) getSqlMapClientTemplate().queryForObject("lockedPipeline", pipelineState.getLockedByPipelineId());
            pipelineState.setLockedBy(lockedBy);
        }
        goCache.put(cacheKey, pipelineState == null ? PipelineState.NOT_LOCKED : pipelineState);
        return pipelineState;
    }
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 33 with TransactionCallback

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

the class PipelineStateDao method lockedPipelines.

public List<String> lockedPipelines() {
    return (List<String>) transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            PropertyProjection pipelineName = Projections.property("pipelineName");
            Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PipelineState.class).setProjection(pipelineName).add(Restrictions.eq("locked", true));
            criteria.setCacheable(false);
            List<String> list = criteria.list();
            return list;
        }
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) PropertyProjection(org.hibernate.criterion.PropertyProjection) TransactionStatus(org.springframework.transaction.TransactionStatus) List(java.util.List) Criteria(org.hibernate.Criteria)

Example 34 with TransactionCallback

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

the class PluginSqlMapDao method getAllPlugins.

// used in tests
@Override
public List<Plugin> getAllPlugins() {
    return (List<Plugin>) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
        Query query = sessionFactory.getCurrentSession().createQuery("FROM " + Plugin.class.getSimpleName());
        query.setCacheable(true);
        return query.list();
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) Query(org.hibernate.Query) List(java.util.List)

Example 35 with TransactionCallback

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

the class PluginSqlMapDao method findPlugin.

@Override
public Plugin findPlugin(final String pluginId) {
    String cacheKey = cacheKeyForPluginSettings(pluginId);
    Plugin plugin = (Plugin) goCache.get(cacheKey);
    if (plugin != null) {
        return plugin;
    }
    synchronized (cacheKey) {
        plugin = (Plugin) goCache.get(cacheKey);
        if (plugin != null) {
            return plugin;
        }
        plugin = (Plugin) transactionTemplate.execute((TransactionCallback) transactionStatus -> sessionFactory.getCurrentSession().createCriteria(Plugin.class).add(Restrictions.eq("pluginId", pluginId)).setCacheable(true).uniqueResult());
        if (plugin != null) {
            goCache.put(cacheKey, plugin);
            return plugin;
        }
        goCache.remove(cacheKey);
        return new NullPlugin();
    }
}
Also used : TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Restrictions(org.hibernate.criterion.Restrictions) Plugin(com.thoughtworks.go.domain.Plugin) SessionFactory(org.hibernate.SessionFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HibernateDaoSupport(org.springframework.orm.hibernate3.support.HibernateDaoSupport) GoCache(com.thoughtworks.go.server.cache.GoCache) Component(org.springframework.stereotype.Component) List(java.util.List) NullPlugin(com.thoughtworks.go.domain.NullPlugin) TransactionStatus(org.springframework.transaction.TransactionStatus) CacheKeyGenerator(com.thoughtworks.go.server.cache.CacheKeyGenerator) Query(org.hibernate.Query) TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionTemplate(com.thoughtworks.go.server.transaction.TransactionTemplate) NullPlugin(com.thoughtworks.go.domain.NullPlugin) Plugin(com.thoughtworks.go.domain.Plugin) NullPlugin(com.thoughtworks.go.domain.NullPlugin)

Aggregations

TransactionCallback (org.springframework.transaction.support.TransactionCallback)101 TransactionStatus (org.springframework.transaction.TransactionStatus)76 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)14 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)11 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)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