List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager unbindResource
public static Object unbindResource(Object key) throws IllegalStateException
From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java
/** * Invoke the specified listener: either as standard MessageListener or (preferably) as SessionAwareMessageListener. * @param channel the Rabbit Channel to operate on * @param message the received Rabbit Message * @throws Exception if thrown by Rabbit API methods * @see #setMessageListener/*from www . j a va2 s . c om*/ */ protected void actualInvokeListener(Channel channel, Message message) throws Exception { Object listener = getMessageListener(); if (listener instanceof ChannelAwareMessageListener) { doInvokeListener((ChannelAwareMessageListener) listener, channel, message); } else if (listener instanceof MessageListener) { boolean bindChannel = isExposeListenerChannel() && isChannelLocallyTransacted(); if (bindChannel) { RabbitResourceHolder resourceHolder = new RabbitResourceHolder(channel, false); resourceHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager.bindResource(this.getConnectionFactory(), resourceHolder); } try { doInvokeListener((MessageListener) listener, message); } finally { if (bindChannel) { // unbind if we bound TransactionSynchronizationManager.unbindResource(this.getConnectionFactory()); } } } else if (listener != null) { throw new FatalListenerExecutionException( "Only MessageListener and SessionAwareMessageListener supported: " + listener); } else { throw new FatalListenerExecutionException( "No message listener specified - see property 'messageListener'"); } }
From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java
/** * Invoke the specified listener as Spring ChannelAwareMessageListener, exposing a new Rabbit Session (potentially * with its own transaction) to the listener if demanded. * @param listener the Spring ChannelAwareMessageListener to invoke * @param channel the Rabbit Channel to operate on * @param message the received Rabbit Message * @throws Exception if thrown by Rabbit API methods or listener itself. * <p>/*from w w w . java 2s .c o m*/ * Exception thrown from listener will be wrapped to {@link ListenerExecutionFailedException}. * @see ChannelAwareMessageListener * @see #setExposeListenerChannel(boolean) */ protected void doInvokeListener(ChannelAwareMessageListener listener, Channel channel, Message message) throws Exception { RabbitResourceHolder resourceHolder = null; Channel channelToUse = channel; boolean boundHere = false; try { if (!isExposeListenerChannel()) { // We need to expose a separate Channel. resourceHolder = getTransactionalResourceHolder(); channelToUse = resourceHolder.getChannel(); /* * If there is a real transaction, the resource will have been bound; otherwise * we need to bind it temporarily here. Any work done on this channel * will be committed in the finally block. */ if (isChannelLocallyTransacted() && !TransactionSynchronizationManager.isActualTransactionActive()) { resourceHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager.bindResource(this.getConnectionFactory(), resourceHolder); boundHere = true; } } else { // if locally transacted, bind the current channel to make it available to RabbitTemplate if (isChannelLocallyTransacted()) { RabbitResourceHolder localResourceHolder = new RabbitResourceHolder(channelToUse, false); localResourceHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager.bindResource(this.getConnectionFactory(), localResourceHolder); boundHere = true; } } // Actually invoke the message listener... try { listener.onMessage(message, channelToUse); } catch (Exception e) { throw wrapToListenerExecutionFailedExceptionIfNeeded(e, message); } } finally { if (resourceHolder != null && boundHere) { // so the channel exposed (because exposeListenerChannel is false) will be closed resourceHolder.setSynchronizedWithTransaction(false); } ConnectionFactoryUtils.releaseResources(resourceHolder); if (boundHere) { // unbind if we bound TransactionSynchronizationManager.unbindResource(this.getConnectionFactory()); if (!isExposeListenerChannel() && isChannelLocallyTransacted()) { /* * commit the temporary channel we exposed; the consumer's channel * will be committed later. Note that when exposing a different channel * when there's no transaction manager, the exposed channel is committed * on each message, and not based on txSize. */ RabbitUtils.commitIfNecessary(channelToUse); } } } }
From source file:org.springframework.data.neo4j.web.support.AsyncRequestInterceptor.java
@Override public <T> void postProcess(NativeWebRequest request, Callable<T> task, Object concurrentResult) { TransactionSynchronizationManager.unbindResource(this.sessionFactory); }
From source file:org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor.java
@Override public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { if (!decrementParticipateCount(request)) { TransactionSynchronizationManager.unbindResource(getSessionFactory()); logger.debug("Closed Neo4j OGM Session in OpenSessionInViewInterceptor"); // close session. // SessionFactoryUtils.closeSession(session); }//w ww . java 2 s. co m }
From source file:org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor.java
@Override public void afterConcurrentHandlingStarted(WebRequest request) { if (!decrementParticipateCount(request)) { TransactionSynchronizationManager.unbindResource(getSessionFactory()); }/*w w w. ja v a2s . c om*/ }
From source file:org.springframework.datastore.mapping.web.support.OpenSessionInViewInterceptor.java
public void afterCompletion(WebRequest webRequest, Exception e) throws Exception { if (hasSessionBound()) { // single session mode SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .unbindResource(getDatastore()); LOG.debug("Closing single Datastore Session in OpenSessionInViewInterceptor"); DatastoreUtils.closeSession(sessionHolder.getSession()); }/*from w ww . j a v a 2 s . c o m*/ }
From source file:org.springframework.jdbc.object.StoredProcedureTests.java
public void testAddInvoicesWithinTransaction() throws Exception { mockCallable.setObject(1, new Integer(1106), Types.INTEGER); ctrlCallable.setVoidCallable();/*from ww w .j a v a2 s . co m*/ mockCallable.setObject(2, new Integer(3), Types.INTEGER); ctrlCallable.setVoidCallable(); mockCallable.registerOutParameter(3, Types.INTEGER); ctrlCallable.setVoidCallable(); mockCallable.execute(); ctrlCallable.setReturnValue(false); mockCallable.getUpdateCount(); ctrlCallable.setReturnValue(-1); mockCallable.getObject(3); ctrlCallable.setReturnValue(new Integer(4)); if (debugEnabled) { mockCallable.getWarnings(); ctrlCallable.setReturnValue(null); } mockCallable.close(); ctrlCallable.setVoidCallable(); mockConnection.prepareCall("{call " + AddInvoice.SQL + "(?, ?, ?)}"); ctrlConnection.setReturnValue(mockCallable); replay(); TransactionSynchronizationManager.bindResource(mockDataSource, new ConnectionHolder(mockConnection)); try { testAddInvoice(1106, 3); } finally { TransactionSynchronizationManager.unbindResource(mockDataSource); } }
From source file:org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor.java
/** * Unbind the Hibernate {@code Session} from the thread and close it). * @see org.springframework.transaction.support.TransactionSynchronizationManager *//* www .java 2 s . c om*/ @Override public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { if (!decrementParticipateCount(request)) { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .unbindResource(getSessionFactory()); logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor"); SessionFactoryUtils.closeSession(sessionHolder.getSession()); } }
From source file:org.springframework.orm.hibernate43.support.OpenSessionInViewInterceptor.java
/** * Unbind the Hibernate {@code Session} from the thread and close it). * @see org.springframework.transaction.support.TransactionSynchronizationManager *///from ww w.j a v a 2s . co m public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { if (!decrementParticipateCount(request)) { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .unbindResource(getSessionFactory()); logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor"); SessionFactoryUtils.closeSession(sessionHolder.getSession()); } }
From source file:org.springframework.orm.hibernate43.support.OpenSessionInViewInterceptor.java
public void afterConcurrentHandlingStarted(WebRequest request) { if (!decrementParticipateCount(request)) { TransactionSynchronizationManager.unbindResource(getSessionFactory()); }//ww w. ja v a 2 s. co m }