當前位置: 首頁>>代碼示例>>Java>>正文


Java TransactionSynchronization類代碼示例

本文整理匯總了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);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:28,代碼來源:SpringJtaSynchronizationAdapter.java

示例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);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:22,代碼來源:JtaAfterCompletionSynchronization.java

示例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");
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:26,代碼來源:LobTypeTests.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:LobTypeTests.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:19,代碼來源:LobTypeTests.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:25,代碼來源:JtaTransactionManagerTests.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:31,代碼來源:JtaTransactionManagerTests.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:26,代碼來源:JtaTransactionManagerTests.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:26,代碼來源:JtaTransactionManagerTests.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:26,代碼來源:JtaTransactionManagerTests.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:25,代碼來源:JtaTransactionManagerTests.java

示例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);
}
 
開發者ID:cucina,項目名稱:opencucina,代碼行數:20,代碼來源:TransactionHandlerImpl.java

示例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));
    }
}
 
開發者ID:alancnet,項目名稱:artifactory,代碼行數:18,代碼來源:SessionSynchronization.java


注:本文中的org.springframework.transaction.support.TransactionSynchronization類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。