本文整理汇总了Java中org.springframework.transaction.support.TransactionSynchronization类的典型用法代码示例。如果您正苦于以下问题:Java TransactionSynchronization类的具体用法?Java TransactionSynchronization怎么用?Java TransactionSynchronization使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TransactionSynchronization类属于org.springframework.transaction.support包,在下文中一共展示了TransactionSynchronization类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: afterCompletion
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
/**
* JTA {@code afterCompletion} callback: invoked after commit/rollback.
* <p>Needs to invoke the Spring synchronization's {@code beforeCompletion}
* at this late stage in case of a rollback, since there is no corresponding
* callback with JTA.
* @see org.springframework.transaction.support.TransactionSynchronization#beforeCompletion
* @see org.springframework.transaction.support.TransactionSynchronization#afterCompletion
*/
@Override
public void afterCompletion(int status) {
if (!this.beforeCompletionCalled) {
// beforeCompletion not called before (probably because of JTA rollback).
// Perform the cleanup here.
this.springSynchronization.beforeCompletion();
}
// Call afterCompletion with the appropriate status indication.
switch (status) {
case Status.STATUS_COMMITTED:
this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
break;
case Status.STATUS_ROLLEDBACK:
this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
break;
default:
this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
}
示例2: afterCompletion
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Override
public void afterCompletion(int status) {
switch (status) {
case Status.STATUS_COMMITTED:
try {
TransactionSynchronizationUtils.invokeAfterCommit(this.synchronizations);
}
finally {
TransactionSynchronizationUtils.invokeAfterCompletion(
this.synchronizations, TransactionSynchronization.STATUS_COMMITTED);
}
break;
case Status.STATUS_ROLLEDBACK:
TransactionSynchronizationUtils.invokeAfterCompletion(
this.synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK);
break;
default:
TransactionSynchronizationUtils.invokeAfterCompletion(
this.synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
}
}
示例3: testClobStringTypeWithFlushOnCommit
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void testClobStringTypeWithFlushOnCommit() throws Exception {
given(lobHandler.getClobAsString(rs, "column")).willReturn("content");
ClobStringType type = new ClobStringType(lobHandler, null);
assertEquals(1, type.sqlTypes().length);
assertEquals(Types.CLOB, type.sqlTypes()[0]);
assertEquals(String.class, type.returnedClass());
assertTrue(type.equals("content", "content"));
assertEquals("content", type.deepCopy("content"));
assertFalse(type.isMutable());
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
TransactionSynchronizationManager.initSynchronization();
try {
type.nullSafeSet(ps, "content", 1);
List synchs = TransactionSynchronizationManager.getSynchronizations();
assertEquals(1, synchs.size());
((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
}
finally {
TransactionSynchronizationManager.clearSynchronization();
}
verify(lobCreator).setClobAsString(ps, 1, "content");
}
示例4: testBlobStringTypeWithNull
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void testBlobStringTypeWithNull() throws Exception {
given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(null);
BlobStringType type = new BlobStringType(lobHandler, null);
assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
TransactionSynchronizationManager.initSynchronization();
try {
type.nullSafeSet(ps, null, 1);
List synchs = TransactionSynchronizationManager.getSynchronizations();
assertEquals(1, synchs.size());
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
}
finally {
TransactionSynchronizationManager.clearSynchronization();
}
verify(lobCreator).setBlobAsBytes(ps, 1, null);
}
示例5: testBlobSerializableTypeWithNull
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void testBlobSerializableTypeWithNull() throws Exception {
given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(null);
BlobSerializableType type = new BlobSerializableType(lobHandler, null);
assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
TransactionSynchronizationManager.initSynchronization();
try {
type.nullSafeSet(ps, null, 1);
List synchs = TransactionSynchronizationManager.getSynchronizations();
assertEquals(1, synchs.size());
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
}
finally {
TransactionSynchronizationManager.clearSynchronization();
}
verify(lobCreator).setBlobAsBytes(ps, 1, null);
}
示例6: onApplicationEvent
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronization transactionSynchronization = createTransactionSynchronization(event);
TransactionSynchronizationManager.registerSynchronization(transactionSynchronization);
}
else if (this.annotation.fallbackExecution()) {
if (this.annotation.phase() == TransactionPhase.AFTER_ROLLBACK) {
logger.warn("Processing '" + event + "' as a fallback execution on AFTER_ROLLBACK phase.");
}
processEvent(event);
}
else {
if (logger.isDebugEnabled()) {
logger.debug("No transaction is running, skipping '" + event + "' for '" + this + "'");
}
}
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:ApplicationListenerMethodTransactionalAdapter.java
示例7: jtaTransactionManagerWithExistingTransactionAndRollbackOnly
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnly() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
示例8: jtaTransactionManagerWithExistingTransactionAndCommitException
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void jtaTransactionManagerWithExistingTransactionAndCommitException() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
willThrow(new OptimisticLockingFailureException("")).given(synch).beforeCommit(false);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
}
});
fail("Should have thrown OptimisticLockingFailureException");
}
catch (OptimisticLockingFailureException ex) {
// expected
}
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
示例9: jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
ptm.setGlobalRollbackOnParticipationFailure(false);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
示例10: jtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
示例11: jtaTransactionManagerWithExistingAndPropagationSupports
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void jtaTransactionManagerWithExistingAndPropagationSupports() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
示例12: jtaTransactionManagerWithPropagationSupports
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Test
public void jtaTransactionManagerWithPropagationSupports() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
示例13: handleStatus
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
private void handleStatus(int status, String type, Serializable... ids) {
Message<?> callmess;
if (TransactionSynchronization.STATUS_COMMITTED == status) {
// TODO make CommitSuccess handle type/ids combo
callmess = MessageBuilder.withPayload(new CommitSuccessEvent(ids))
.setHeader(Operative.DESTINATION_NAME, "server.queue").build();
} else {
CompensateEvent compensateEvent = new CompensateEvent(status);
compensateEvent.setIds(ids);
compensateEvent.setType(type);
callmess = MessageBuilder.withPayload(compensateEvent)
.setHeader(Operative.DESTINATION_NAME, "server.queue").build();
LOG.debug("Compensating for " + type + ":" + Arrays.toString(ids));
}
asyncChannel.send(callmess);
}
示例14: releaseWithTransaction
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
private void releaseWithTransaction(final LockInstance lockInstance) {
TransactionLockSynchronization synchronization = null;
for (TransactionSynchronization transactionSynchronization : TransactionSynchronizationManager
.getSynchronizations()) {
if (transactionSynchronization instanceof TransactionLockSynchronization) {
synchronization = (TransactionLockSynchronization) transactionSynchronization;
}
}
if (null == synchronization) {
synchronization = new TransactionLockSynchronization(this);
TransactionSynchronizationManager.registerSynchronization(synchronization);
}
synchronization.add(lockInstance);
}
开发者ID:My-easy-Solutions,项目名称:spring-lock,代码行数:17,代码来源:SessionTransactedLockSynchronizationManager.java
示例15: afterCompletion
import org.springframework.transaction.support.TransactionSynchronization; //导入依赖的package包/类
@Override
public void afterCompletion(int status) {
if (sessionActive) {
boolean success = status == TransactionSynchronization.STATUS_COMMITTED;
// Commit the locks/discard changes on rollback
try {
session.afterCompletion(success);
} finally {
session.releaseResources();
StorageSessionHolder.removeSession();
}
}
if (log.isDebugEnabled()) {
log.debug("Session completed: '{}' in {}",
sessionName, TimeUnitFormat.getTimeString(System.nanoTime() - startTime));
}
}