Search in sources :

Example 6 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project spring-framework by spring-projects.

the class DataSourceTransactionManagerTests method testParticipatingTransactionWithTransactionStartedFromSynch.

@Test
public void testParticipatingTransactionWithTransactionStartedFromSynch() throws Exception {
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_COMMITTED) {

        @Override
        protected void doAfterCompletion(int status) {
            super.doAfterCompletion(status);
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                }
            });
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            });
        }
    };
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            TransactionSynchronizationManager.registerSynchronization(synch);
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue(synch.beforeCommitCalled);
    assertTrue(synch.beforeCompletionCalled);
    assertTrue(synch.afterCommitCalled);
    assertTrue(synch.afterCompletionCalled);
    assertTrue(synch.afterCompletionException instanceof IllegalStateException);
    verify(con, times(2)).commit();
    verify(con, times(2)).close();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 7 with TransactionSynchronizationAdapter

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

the class UserSqlMapDao method saveOrUpdate.

public void saveOrUpdate(final User user) {
    assertUserNotAnonymous(user);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    clearEnabledUserCountFromCache();
                }
            });
            sessionFactory.getCurrentSession().saveOrUpdate(copyLoginToDisplayNameIfNotPresent(user));
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 8 with TransactionSynchronizationAdapter

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

the class PipelineTimeline method update.

public void update() {
    acquireAllWriteLocks();
    try {
        final long maximumIdBeforeUpdate = maximumId;
        transactionTemplate.execute(new TransactionCallback() {

            public Object doInTransaction(TransactionStatus 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 : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) PipelineTimelineEntry(com.thoughtworks.go.domain.PipelineTimelineEntry) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 9 with TransactionSynchronizationAdapter

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

the class JobInstanceSqlMapDao method updateStateAndResult.

public JobInstance updateStateAndResult(final JobInstance jobInstance) {
    return (JobInstance) transactionTemplate.execute(new TransactionCallback() {

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

                @Override
                public void afterCommit() {
                    // Methods not extracted in order to make synchronization visible.
                    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) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 10 with TransactionSynchronizationAdapter

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

the class StageSqlMapDao method updateResult.

public void updateResult(final Stage stage, final StageResult result) {
    transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

        @Override
        public void afterCommit() {
            StageIdentifier identifier = stage.getIdentifier();
            clearStageHistoryPageCaches(stage, identifier.getPipelineName(), true);
            clearJobStatusDependentCaches(stage.getId(), identifier);
            removeFromCache(cacheKeyForStageCountForGraph(identifier.getPipelineName(), identifier.getStageName()));
        }
    });
    getSqlMapClientTemplate().update("updateStageStatus", arguments("stageId", stage.getId()).and("result", result.toString()).and("state", stage.getState()).and("completedByTransitionId", stage.getCompletedByTransitionId()).asMap());
    upddateLastTransitionedTime(stage);
}
Also used : TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Aggregations

TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)34 Test (org.junit.Test)16 TransactionStatus (org.springframework.transaction.TransactionStatus)15 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)14 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 Pipeline (com.thoughtworks.go.domain.Pipeline)3 SystemException (javax.transaction.SystemException)3 PipelineState (com.thoughtworks.go.domain.PipelineState)2 GitCommand (com.thoughtworks.go.domain.materials.git.GitCommand)2 TransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TransactionSynchronizationManager)2 File (java.io.File)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 SimpleTransactionStatus (org.springframework.transaction.support.SimpleTransactionStatus)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2