Search in sources :

Example 81 with TransactionCallback

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

the class PipelineSqlMapDao method save.

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

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

                @Override
                public void afterCommit() {
                    goCache.remove(cacheKeyForLatestPipelineIdByPipelineName(pipeline.getName()));
                    invalidateCacheConditionallyForPipelineInstancesTriggeredWithDependencyMaterial(pipeline);
                }
            });
            pipelineByBuildIdCache.flushOnCommit();
            getSqlMapClientTemplate().insert("insertPipeline", pipeline);
            savePipelineMaterialRevisions(pipeline, pipeline.getId());
            environmentVariableDao.save(pipeline.getId(), 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)

Example 82 with TransactionCallback

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

the class UserSqlMapDao method findNotificationSubscribingUsers.

@Override
public Users findNotificationSubscribingUsers() {
    return (Users) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
        criteria.setCacheable(true);
        criteria.add(Restrictions.isNotEmpty("notificationFilters"));
        criteria.add(Restrictions.eq("enabled", true));
        return new Users(criteria.list());
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) Users(com.thoughtworks.go.domain.Users) Criteria(org.hibernate.Criteria)

Example 83 with TransactionCallback

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

the class UserSqlMapDao method deleteUser.

@Override
public boolean deleteUser(final String username, String byWhom) {
    return (Boolean) transactionTemplate.execute((TransactionCallback) status -> {
        User user = findUser(username);
        if (user instanceof NullUser) {
            throw new RecordNotFoundException(EntityType.User, username);
        }
        if (user.isEnabled()) {
            throw new UserEnabledException();
        }
        sessionFactory.getCurrentSession().delete(user);
        accessTokenDao.revokeTokensBecauseOfUserDelete(Collections.singletonList(username), byWhom);
        return Boolean.TRUE;
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) NullUser(com.thoughtworks.go.domain.NullUser) User(com.thoughtworks.go.domain.User) NullUser(com.thoughtworks.go.domain.NullUser) UserEnabledException(com.thoughtworks.go.server.exceptions.UserEnabledException)

Example 84 with TransactionCallback

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

the class AgentMutex method getAgentsByUUIDs.

public List<Agent> getAgentsByUUIDs(List<String> uuids) {
    AgentMutex mutex = agentMutexes.acquire(uuids);
    synchronized (mutex) {
        List<Agent> agents = (List<Agent>) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
            Query query = sessionFactory.getCurrentSession().createQuery("FROM Agent where uuid in :uuids and deleted = false");
            query.setCacheable(true);
            query.setParameterList("uuids", uuids);
            return query.list();
        });
        agentMutexes.release(uuids, mutex);
        return agents;
    }
}
Also used : Agent(com.thoughtworks.go.config.Agent) TransactionCallback(org.springframework.transaction.support.TransactionCallback) Query(org.hibernate.Query) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Example 85 with TransactionCallback

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

the class AgentMutex method fetchAgentFromDBByUUIDIncludingDeleted.

public Agent fetchAgentFromDBByUUIDIncludingDeleted(final String uuid) {
    List<String> uuids = singletonList(uuid);
    AgentMutex mutex = agentMutexes.acquire(uuids);
    synchronized (mutex) {
        Agent agent = (Agent) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
            Query query = sessionFactory.getCurrentSession().createQuery("FROM Agent where uuid = :uuid");
            query.setCacheable(true);
            query.setParameter("uuid", uuid);
            return query.uniqueResult();
        });
        agentMutexes.release(uuids, mutex);
        return agent;
    }
}
Also used : Agent(com.thoughtworks.go.config.Agent) TransactionCallback(org.springframework.transaction.support.TransactionCallback) Query(org.hibernate.Query)

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