List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager getResource
@Nullable public static Object getResource(Object key)
From source file:org.springframework.datastore.mapping.core.DatastoreUtils.java
/** * Get a Datastore Session for the given Datastore. Is aware of and will * return any existing corresponding Session bound to the current thread, for * example when using {@link org.springframework.datastore.mapping.transactions.DatastoreTransactionManager}. Will create a new * Session otherwise, if "allowCreate" is <code>true</code>. * * @param datastore Datastore to create the session with * Session on transaction synchronization (may be <code>null</code>) * @param allowCreate whether a non-transactional Session should be created * when no transactional Session can be found for the current thread * @return the Datastore Session/*from w ww.j a v a 2 s . co m*/ * @throws IllegalStateException if no thread-bound Session found and * "allowCreate" is <code>false</code> */ private static Session doGetSession(Datastore datastore, boolean allowCreate) { Assert.notNull(datastore, "No Datastore specified"); SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(datastore); if (sessionHolder != null && !sessionHolder.isEmpty()) { // pre-bound Datastore Session Session session; if (TransactionSynchronizationManager.isSynchronizationActive() && sessionHolder.doesNotHoldNonDefaultSession()) { // Spring transaction management is active -> // register pre-bound Session with it for transactional flushing. session = sessionHolder.getSession(); if (session != null && !sessionHolder.isSynchronizedWithTransaction()) { logger.debug("Registering Spring transaction synchronization for existing Datastore Session"); TransactionSynchronizationManager.registerSynchronization( new SpringSessionSynchronization(sessionHolder, datastore, false)); sessionHolder.setSynchronizedWithTransaction(true); } if (session != null) { return session; } } } logger.debug("Opening Datastore Session"); Session session = datastore.connect(); // Use same Session for further Datastore actions within the transaction. // Thread object will get removed by synchronization at transaction completion. if (TransactionSynchronizationManager.isSynchronizationActive()) { // We're within a Spring-managed transaction, possibly from JtaTransactionManager. logger.debug("Registering Spring transaction synchronization for new Datastore Session"); SessionHolder holderToUse = sessionHolder; if (holderToUse == null) { holderToUse = new SessionHolder(session); } else { holderToUse.addSession(session); } TransactionSynchronizationManager .registerSynchronization(new SpringSessionSynchronization(holderToUse, datastore, true)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sessionHolder) { TransactionSynchronizationManager.bindResource(datastore, holderToUse); } } // Check whether we are allowed to return the Session. if (!allowCreate && !isSessionTransactional(session, datastore)) { closeSession(session); throw new IllegalStateException("No Datastore Session bound to thread, " + "and configuration does not allow creation of non-transactional one here"); } return session; }
From source file:org.springframework.datastore.mapping.web.support.OpenSessionInViewInterceptor.java
public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception { // Only potentially flush in single session mode. if (hasSessionBound()) { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(getDatastore()); LOG.debug("Flushing single Datastore Session in OpenSessionInViewInterceptor"); final Session session = sessionHolder.getSession(); if (session.getFlushMode() == FlushModeType.AUTO) { session.flush();/*from w w w. j a v a 2 s . c o m*/ } } }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testCommit() { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); syncProcessor.setBeanFactory(mock(BeanFactory.class)); PollableChannel queueChannel = new QueueChannel(); syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix")); syncProcessor.setBeforeCommitChannel(queueChannel); syncProcessor.setAfterCommitExpression(new SpelExpressionParser().parseExpression("#baz")); DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory( syncProcessor);//from ww w . j a v a 2 s . c om adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); IntegrationResourceHolder holder = (IntegrationResourceHolder) TransactionSynchronizationManager .getResource(this); holder.addAttribute("baz", "qux"); holder.addAttribute("bix", "qox"); return message; } }); MessageChannel afterCommitChannel = TestUtils.getPropertyValue(syncProcessor, "afterCommitChannel", MessageChannel.class); assertThat(afterCommitChannel, Matchers.instanceOf(NullChannel.class)); Log logger = TestUtils.getPropertyValue(afterCommitChannel, "logger", Log.class); logger = Mockito.spy(logger); Mockito.when(logger.isDebugEnabled()).thenReturn(true); DirectFieldAccessor dfa = new DirectFieldAccessor(afterCommitChannel); dfa.setPropertyValue("logger", logger); TransactionSynchronizationManager.initSynchronization(); TransactionSynchronizationManager.setActualTransactionActive(true); doPoll(adapter); TransactionSynchronizationUtils.triggerBeforeCommit(false); TransactionSynchronizationUtils.triggerAfterCommit(); Message<?> beforeCommitMessage = queueChannel.receive(1000); assertNotNull(beforeCommitMessage); assertEquals("qox", beforeCommitMessage.getPayload()); Mockito.verify(logger).debug(Mockito.anyString()); TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED); TransactionSynchronizationManager.clearSynchronization(); TransactionSynchronizationManager.setActualTransactionActive(false); }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testTransactionSynchronizationFactoryBean() { ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(TestTxSyncConfiguration.class); TransactionSynchronizationFactory syncFactory = ctx.getBean(TransactionSynchronizationFactory.class); PollableChannel queueChannel = ctx.getBean("outputChannel", PollableChannel.class); SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override//from w w w . j a v a 2 s . c o m public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); IntegrationResourceHolder holder = (IntegrationResourceHolder) TransactionSynchronizationManager .getResource(this); holder.addAttribute("baz", "qux"); holder.addAttribute("bix", "qox"); return message; } }); TransactionSynchronizationManager.initSynchronization(); TransactionSynchronizationManager.setActualTransactionActive(true); doPoll(adapter); TransactionSynchronizationUtils.triggerBeforeCommit(false); TransactionSynchronizationUtils.triggerAfterCommit(); Message<?> beforeCommitMessage = queueChannel.receive(1000); assertNotNull(beforeCommitMessage); assertEquals("qox", beforeCommitMessage.getPayload()); Message<?> afterCommitMessage = queueChannel.receive(1000); assertNotNull(afterCommitMessage); assertEquals("qux", afterCommitMessage.getPayload()); TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED); TransactionSynchronizationManager.clearSynchronization(); TransactionSynchronizationManager.setActualTransactionActive(false); ctx.close(); }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testRollback() { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); syncProcessor.setBeanFactory(mock(BeanFactory.class)); PollableChannel queueChannel = new QueueChannel(); syncProcessor.setAfterRollbackChannel(queueChannel); syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz")); DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory( syncProcessor);//from w ww . j a v a 2s . co m adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); ((IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this)) .addAttribute("baz", "qux"); return message; } }); TransactionSynchronizationManager.initSynchronization(); TransactionSynchronizationManager.setActualTransactionActive(true); doPoll(adapter); TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); Message<?> rollbackMessage = queueChannel.receive(1000); assertNotNull(rollbackMessage); assertEquals("qux", rollbackMessage.getPayload()); TransactionSynchronizationManager.clearSynchronization(); TransactionSynchronizationManager.setActualTransactionActive(false); }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testCommitWithManager() { final PollableChannel queueChannel = new QueueChannel(); TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager()); transactionTemplate.execute(status -> { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix")); syncProcessor.setBeforeCommitChannel(queueChannel); syncProcessor.setAfterCommitChannel(queueChannel); syncProcessor.setAfterCommitExpression(new SpelExpressionParser().parseExpression("#baz")); DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory( syncProcessor);/*from w ww . ja v a2 s .co m*/ adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); IntegrationResourceHolder holder = (IntegrationResourceHolder) TransactionSynchronizationManager .getResource(this); holder.addAttribute("baz", "qux"); holder.addAttribute("bix", "qox"); return message; } }); doPoll(adapter); return null; }); Message<?> beforeCommitMessage = queueChannel.receive(1000); assertNotNull(beforeCommitMessage); assertEquals("qox", beforeCommitMessage.getPayload()); Message<?> afterCommitMessage = queueChannel.receive(1000); assertNotNull(afterCommitMessage); assertEquals("qux", afterCommitMessage.getPayload()); }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testRollbackWithManager() { final PollableChannel queueChannel = new QueueChannel(); TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager()); try {/*from w w w . j a v a2 s . com*/ transactionTemplate.execute(status -> { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setAfterRollbackChannel(queueChannel); syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz")); DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory( syncProcessor); adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); ((IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this)) .addAttribute("baz", "qux"); return message; } }); doPoll(adapter); throw new RuntimeException("Force rollback"); }); } catch (Exception e) { assertEquals("Force rollback", e.getMessage()); } Message<?> rollbackMessage = queueChannel.receive(1000); assertNotNull(rollbackMessage); assertEquals("qux", rollbackMessage.getPayload()); }
From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java
@Test public void testRollbackWithManagerUsingStatus() { final PollableChannel queueChannel = new QueueChannel(); TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager()); transactionTemplate.execute(status -> { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setAfterRollbackChannel(queueChannel); syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz")); DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory( syncProcessor);/*from w ww . j a va 2s. c o m*/ adapter.setTransactionSynchronizationFactory(syncFactory); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); adapter.setSource(new MessageSource<String>() { @Override public Message<String> receive() { GenericMessage<String> message = new GenericMessage<String>("foo"); ((IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this)) .addAttribute("baz", "qux"); return message; } }); doPoll(adapter); status.setRollbackOnly(); return null; }); Message<?> rollbackMessage = queueChannel.receive(1000); assertNotNull(rollbackMessage); assertEquals("qux", rollbackMessage.getPayload()); }
From source file:org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory.java
public TransactionSynchronization create(Object key) { Assert.notNull(key, "'key' must not be null"); Object resourceHolder = TransactionSynchronizationManager.getResource(key); Assert.isInstanceOf(IntegrationResourceHolder.class, resourceHolder); return new DefaultTransactionalResourceSynchronization((IntegrationResourceHolder) resourceHolder, key); }
From source file:org.springframework.jca.cci.connection.ConnectionFactoryUtils.java
/** * Actually obtain a CCI Connection from the given ConnectionFactory. * Same as {@link #getConnection}, but throwing the original ResourceException. * <p>Is aware of a corresponding Connection bound to the current thread, for example * when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread * if transaction synchronization is active (e.g. if in a JTA transaction). * <p>Directly accessed by {@link TransactionAwareConnectionFactoryProxy}. * @param cf the ConnectionFactory to obtain Connection from * @return a CCI Connection from the given ConnectionFactory * @throws ResourceException if thrown by CCI API methods * @see #doReleaseConnection// ww w . j a v a 2 s.c o m */ public static Connection doGetConnection(ConnectionFactory cf) throws ResourceException { Assert.notNull(cf, "No ConnectionFactory specified"); ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(cf); if (conHolder != null) { return conHolder.getConnection(); } logger.debug("Opening CCI Connection"); Connection con = cf.getConnection(); if (TransactionSynchronizationManager.isSynchronizationActive()) { logger.debug("Registering transaction synchronization for CCI Connection"); conHolder = new ConnectionHolder(con); conHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager.registerSynchronization(new ConnectionSynchronization(conHolder, cf)); TransactionSynchronizationManager.bindResource(cf, conHolder); } return con; }