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();
}
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));
}
});
}
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();
}
}
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;
}
});
}
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);
}
Aggregations