Search in sources :

Example 36 with TransactionCallback

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

the class PipelineTimeline method update.

public void update() {
    acquireAllWriteLocks();
    try {
        final long maximumIdBeforeUpdate = maximumId;
        transactionTemplate.execute((TransactionCallback) transactionStatus -> {
            final List<PipelineTimelineEntry> newlyAddedEntries = new ArrayList<>();
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCompletion(int status) {
                    if (STATUS_ROLLED_BACK == status) {
                        rollbackTempEntries();
                    } else if (STATUS_COMMITTED == status) {
                        notifyListeners(newlyAddedEntries);
                    }
                }

                private void rollbackTempEntries() {
                    for (PipelineTimelineEntry entry : newlyAddedEntries) {
                        rollbackNewEntryFor(entry);
                    }
                    maximumId = maximumIdBeforeUpdate;
                }

                private void rollbackNewEntryFor(PipelineTimelineEntry entry) {
                    CaseInsensitiveString pipelineName = new CaseInsensitiveString(entry.getPipelineName());
                    initializedNaturalOrderCollection(pipelineName).remove(entry);
                    initializedScheduleOrderCollection(pipelineName).remove(entry);
                }
            });
            pipelineRepository.updatePipelineTimeline(PipelineTimeline.this, newlyAddedEntries);
            return null;
        });
    } finally {
        releaseAllWriteLocks();
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) PipelineTimelineEntry(com.thoughtworks.go.domain.PipelineTimelineEntry) Cloner(com.rits.cloning.Cloner) PipelineRepository(com.thoughtworks.go.server.persistence.PipelineRepository) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) TimelineUpdateListener(com.thoughtworks.go.listener.TimelineUpdateListener) TestOnly(org.jetbrains.annotations.TestOnly) Component(org.springframework.stereotype.Component) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionCallback(org.springframework.transaction.support.TransactionCallback) ClonerFactory(com.thoughtworks.go.util.ClonerFactory) TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) TransactionTemplate(com.thoughtworks.go.server.transaction.TransactionTemplate) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) PipelineTimelineEntry(com.thoughtworks.go.domain.PipelineTimelineEntry) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 37 with TransactionCallback

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

the class UserSqlMapDao method deleteUsers.

@Override
public boolean deleteUsers(List<String> userNames, String byWhom) {
    return (Boolean) transactionTemplate.execute((TransactionCallback) status -> {
        String queryString = "delete from User where name in (:userNames)";
        Query query = sessionFactory.getCurrentSession().createQuery(queryString);
        query.setParameterList("userNames", userNames);
        query.executeUpdate();
        accessTokenDao.revokeTokensBecauseOfUserDelete(userNames, byWhom);
        return Boolean.TRUE;
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) Query(org.hibernate.Query)

Example 38 with TransactionCallback

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

the class EnvironmentVariableSqlMapDao method load.

@Override
public EnvironmentVariables load(final Long entityId, final EnvironmentVariableType type) {
    List<EnvironmentVariable> result = (List<EnvironmentVariable>) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EnvironmentVariable.class).add(Restrictions.eq("entityId", entityId)).add(Restrictions.eq("entityType", type.toString())).addOrder(Order.asc("id"));
        criteria.setCacheable(true);
        return criteria.list();
    });
    return new EnvironmentVariables(result);
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) EnvironmentVariables(com.thoughtworks.go.domain.EnvironmentVariables) EnvironmentVariable(com.thoughtworks.go.domain.EnvironmentVariable) List(java.util.List) Criteria(org.hibernate.Criteria)

Example 39 with TransactionCallback

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

the class JobInstanceSqlMapDao method updateStateAndResult.

@Override
public JobInstance updateStateAndResult(final JobInstance jobInstance) {
    return (JobInstance) transactionTemplate.execute((TransactionCallback) status -> {
        transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

            @Override
            public void afterCommit() {
                synchronized (cacheKeyForJobPlan(jobInstance.getId())) {
                    removeCachedJobPlan(jobInstance);
                }
                synchronized (cacheKeyForActiveJobIds()) {
                    goCache.remove(cacheKeyForActiveJobIds());
                }
                String activeJobKey = cacheKeyForActiveJob(jobInstance.getId());
                synchronized (activeJobKey) {
                    goCache.remove(activeJobKey);
                }
                removeCachedJobInstance(jobInstance);
            }
        });
        logIfJobIsCompleted(jobInstance);
        updateStatus(jobInstance);
        updateResult(jobInstance);
        return jobInstance;
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 40 with TransactionCallback

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

the class MaterialRepositoryIntegrationTest method shouldAllowSavingModificationsIfRevisionsAcrossDifferentMaterialsHappenToBeSame.

@Test
public void shouldAllowSavingModificationsIfRevisionsAcrossDifferentMaterialsHappenToBeSame() {
    final MaterialInstance materialInstance1 = repo.findOrCreateFrom(new GitMaterial(UUID.randomUUID().toString(), "branch"));
    final MaterialInstance materialInstance2 = repo.findOrCreateFrom(new GitMaterial(UUID.randomUUID().toString(), "branch"));
    final ArrayList<Modification> modificationsForFirstMaterial = getModifications(3);
    transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            repo.saveModifications(materialInstance1, modificationsForFirstMaterial);
            return null;
        }
    });
    assertThat(repo.getModificationsFor(materialInstance1, Pagination.pageByNumber(1, 10, 10)).size(), is(3));
    final ArrayList<Modification> modificationsForSecondMaterial = getModifications(3);
    transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            repo.saveModifications(materialInstance2, modificationsForSecondMaterial);
            return null;
        }
    });
    Modifications modificationsFromDb = repo.getModificationsFor(materialInstance2, Pagination.pageByNumber(1, 10, 10));
    assertThat(modificationsFromDb.size(), is(3));
    for (Modification modification : modificationsForSecondMaterial) {
        assertThat(modificationsFromDb.containsRevisionFor(modification), is(true));
    }
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) TransactionStatus(org.springframework.transaction.TransactionStatus) P4MaterialInstance(com.thoughtworks.go.domain.materials.perforce.P4MaterialInstance) PackageMaterialInstance(com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance) TfsMaterialInstance(com.thoughtworks.go.domain.materials.tfs.TfsMaterialInstance) HgMaterialInstance(com.thoughtworks.go.domain.materials.mercurial.HgMaterialInstance) PluggableSCMMaterialInstance(com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance) SvnMaterialInstance(com.thoughtworks.go.domain.materials.svn.SvnMaterialInstance) Test(org.junit.jupiter.api.Test)

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