List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager getResource
@Nullable public static Object getResource(Object key)
From source file:org.springframework.jca.cci.connection.ConnectionFactoryUtils.java
/** * Determine whether the given JCA CCI Connection is transactional, that is, * bound to the current thread by Spring's transaction facilities. * @param con the Connection to check//from w w w . j av a 2s. c o m * @param cf the ConnectionFactory that the Connection was obtained from * (may be {@code null}) * @return whether the Connection is transactional */ public static boolean isConnectionTransactional(Connection con, @Nullable ConnectionFactory cf) { if (cf == null) { return false; } ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(cf); return (conHolder != null && conHolder.getConnection() == con); }
From source file:org.springframework.jdbc.datasource.DataSourceUtils.java
/** * Actually obtain a JDBC Connection from the given DataSource. * Same as {@link #getConnection}, but throwing the original SQLException. * <p>Is aware of a corresponding Connection bound to the current thread, for example * when using {@link DataSourceTransactionManager}. Will bind a Connection to the thread * if transaction synchronization is active (e.g. if in a JTA transaction). * <p>Directly accessed by {@link TransactionAwareDataSourceProxy}. * @param dataSource the DataSource to obtain Connections from * @return a JDBC Connection from the given DataSource * @throws SQLException if thrown by JDBC methods * @see #doReleaseConnection//from ww w . j ava2 s . com */ public static Connection doGetConnection(DataSource dataSource) throws SQLException { Assert.notNull(dataSource, "No DataSource specified"); ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) { conHolder.requested(); if (!conHolder.hasConnection()) { logger.debug("Fetching resumed JDBC Connection from DataSource"); conHolder.setConnection(fetchConnection(dataSource)); } return conHolder.getConnection(); } // Else we either got no holder or an empty thread-bound holder here. logger.debug("Fetching JDBC Connection from DataSource"); Connection con = fetchConnection(dataSource); if (TransactionSynchronizationManager.isSynchronizationActive()) { logger.debug("Registering transaction synchronization for JDBC Connection"); // Use same Connection for further JDBC actions within the transaction. // Thread-bound object will get removed by synchronization at transaction completion. ConnectionHolder holderToUse = conHolder; if (holderToUse == null) { holderToUse = new ConnectionHolder(con); } else { holderToUse.setConnection(con); } holderToUse.requested(); TransactionSynchronizationManager .registerSynchronization(new ConnectionSynchronization(holderToUse, dataSource)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != conHolder) { TransactionSynchronizationManager.bindResource(dataSource, holderToUse); } } return con; }
From source file:org.springframework.jdbc.datasource.DataSourceUtils.java
/** * Determine whether the given JDBC Connection is transactional, that is, * bound to the current thread by Spring's transaction facilities. * @param con the Connection to check// w w w. ja v a 2 s. c o m * @param dataSource the DataSource that the Connection was obtained from * (may be {@code null}) * @return whether the Connection is transactional */ public static boolean isConnectionTransactional(Connection con, @Nullable DataSource dataSource) { if (dataSource == null) { return false; } ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); return (conHolder != null && connectionEquals(conHolder, con)); }
From source file:org.springframework.jdbc.datasource.DataSourceUtils.java
/** * Apply the specified timeout - overridden by the current transaction timeout, * if any - to the given JDBC Statement object. * @param stmt the JDBC Statement object * @param dataSource the DataSource that the Connection was obtained from * @param timeout the timeout to apply (or 0 for no timeout outside of a transaction) * @throws SQLException if thrown by JDBC methods * @see java.sql.Statement#setQueryTimeout *//*w ww .j a va 2s . c o m*/ public static void applyTimeout(Statement stmt, @Nullable DataSource dataSource, int timeout) throws SQLException { Assert.notNull(stmt, "No Statement specified"); ConnectionHolder holder = null; if (dataSource != null) { holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); } if (holder != null && holder.hasTimeout()) { // Remaining transaction timeout overrides specified value. stmt.setQueryTimeout(holder.getTimeToLiveInSeconds()); } else if (timeout >= 0) { // No current transaction timeout -> apply specified value. stmt.setQueryTimeout(timeout); } }
From source file:org.springframework.jdbc.datasource.DataSourceUtils.java
/** * Actually close the given Connection, obtained from the given DataSource. * Same as {@link #releaseConnection}, but throwing the original SQLException. * <p>Directly accessed by {@link TransactionAwareDataSourceProxy}. * @param con the Connection to close if necessary * (if this is {@code null}, the call will be ignored) * @param dataSource the DataSource that the Connection was obtained from * (may be {@code null})/*from w w w. ja v a 2 s.c o m*/ * @throws SQLException if thrown by JDBC methods * @see #doGetConnection */ public static void doReleaseConnection(@Nullable Connection con, @Nullable DataSource dataSource) throws SQLException { if (con == null) { return; } if (dataSource != null) { ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager .getResource(dataSource); if (conHolder != null && connectionEquals(conHolder, con)) { // It's the transactional Connection: Don't close it. conHolder.released(); return; } } logger.debug("Returning JDBC Connection to DataSource"); doCloseConnection(con, dataSource); }
From source file:org.springframework.jms.connection.ConnectionFactoryUtils.java
/** * Determine whether the given JMS Session is transactional, that is, * bound to the current thread by Spring's transaction facilities. * @param session the JMS Session to check * @param cf the JMS ConnectionFactory that the Session originated from * @return whether the Session is transactional *//*from w w w.ja v a2s .c o m*/ public static boolean isSessionTransactional(@Nullable Session session, @Nullable ConnectionFactory cf) { if (session == null || cf == null) { return false; } JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager.getResource(cf); return (resourceHolder != null && resourceHolder.containsSession(session)); }
From source file:org.springframework.jms.connection.ConnectionFactoryUtils.java
/** * Obtain a JMS Session that is synchronized with the current transaction, if any. * @param connectionFactory the JMS ConnectionFactory to bind for * (used as TransactionSynchronizationManager key) * @param resourceFactory the ResourceFactory to use for extracting or creating * JMS resources//from w ww .jav a 2 s. c o m * @param startConnection whether the underlying JMS Connection approach should be * started in order to allow for receiving messages. Note that a reused Connection * may already have been started before, even if this flag is {@code false}. * @return the transactional Session, or {@code null} if none found * @throws JMSException in case of JMS failure */ @Nullable public static Session doGetTransactionalSession(ConnectionFactory connectionFactory, ResourceFactory resourceFactory, boolean startConnection) throws JMSException { Assert.notNull(connectionFactory, "ConnectionFactory must not be null"); Assert.notNull(resourceFactory, "ResourceFactory must not be null"); JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager .getResource(connectionFactory); if (resourceHolder != null) { Session session = resourceFactory.getSession(resourceHolder); if (session != null) { if (startConnection) { Connection con = resourceFactory.getConnection(resourceHolder); if (con != null) { con.start(); } } return session; } if (resourceHolder.isFrozen()) { return null; } } if (!TransactionSynchronizationManager.isSynchronizationActive()) { return null; } JmsResourceHolder resourceHolderToUse = resourceHolder; if (resourceHolderToUse == null) { resourceHolderToUse = new JmsResourceHolder(connectionFactory); } Connection con = resourceFactory.getConnection(resourceHolderToUse); Session session = null; try { boolean isExistingCon = (con != null); if (!isExistingCon) { con = resourceFactory.createConnection(); resourceHolderToUse.addConnection(con); } session = resourceFactory.createSession(con); resourceHolderToUse.addSession(session, con); if (startConnection) { con.start(); } } catch (JMSException ex) { if (session != null) { try { session.close(); } catch (Throwable ex2) { // ignore } } if (con != null) { try { con.close(); } catch (Throwable ex2) { // ignore } } throw ex; } if (resourceHolderToUse != resourceHolder) { TransactionSynchronizationManager.registerSynchronization(new JmsResourceSynchronization( resourceHolderToUse, connectionFactory, resourceFactory.isSynchedLocalTransactionAllowed())); resourceHolderToUse.setSynchronizedWithTransaction(true); TransactionSynchronizationManager.bindResource(connectionFactory, resourceHolderToUse); } return session; }
From source file:org.springframework.jms.core.JmsTemplate.java
/** * Execute the action specified by the given action object within a * JMS Session. Generalized version of execute(SessionCallback), * allowing to start the JMS Connection on the fly. * <p>Use execute(SessionCallback) for the general case. Starting * the JMS Connection is just necessary for receiving messages, * which is preferably achieve through the <code>receive</code> methods. * @param action callback object that exposes the session * @return the result object from working with the session * @throws JmsException if there is any problem * @see #execute(SessionCallback)/*from www. j ava 2 s. c om*/ * @see #receive */ public Object execute(SessionCallback action, boolean startConnection) throws JmsException { Connection con = null; Session session = null; try { Connection conToUse = null; Session sessionToUse = null; ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager .getResource(getConnectionFactory()); if (conHolder != null) { conToUse = conHolder.getConnection(); if (startConnection) { conToUse.start(); } sessionToUse = conHolder.getSession(); } else { //connection con = createConnection(); if (startConnection) { con.start(); } //session session = createSession(con); conToUse = con; sessionToUse = session; } if (logger.isDebugEnabled()) { logger.debug("Executing callback on JMS session [" + sessionToUse + "] from connection [" + conToUse + "]"); } // return action.doInJms(sessionToUse); } catch (JMSException ex) { throw convertJmsAccessException(ex); } finally { JmsUtils.closeSession(session); JmsUtils.closeConnection(con); } }
From source file:org.springframework.jms.core.JmsTemplate.java
protected Message doReceive(Session session, Destination destination) throws JMSException { MessageConsumer consumer = createConsumer(session, destination); try {/*from ww w.j a va 2 s. c o m*/ // use transaction timeout if available long timeout = getReceiveTimeout(); ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager .getResource(getConnectionFactory()); if (conHolder != null && conHolder.hasTimeout()) { timeout = conHolder.getTimeToLiveInMillis(); } Message message = (timeout >= 0) ? consumer.receive(timeout) : consumer.receive(); if (session.getTransacted()) { if (conHolder == null) { // transacted session created by this template -> commit session.commit(); } } else if (message != null && isClientAcknowledge(session)) { message.acknowledge(); } return message; } finally { JmsUtils.closeMessageConsumer(consumer); } }
From source file:org.springframework.ldap.transaction.compensating.manager.TransactionAwareDirContextInvocationHandler.java
/** * Close the supplied context, but only if it is not associated with the * current transaction.//from ww w . java 2 s.c o m * * @param context * the DirContext to close. * @param contextSource * the ContextSource bound to the transaction. * @throws NamingException */ void doCloseConnection(DirContext context, ContextSource contextSource) throws javax.naming.NamingException { DirContextHolder transactionContextHolder = (DirContextHolder) TransactionSynchronizationManager .getResource(contextSource); if (transactionContextHolder == null || transactionContextHolder.getCtx() != context) { log.debug("Closing context"); // This is not the transactional context or the transaction is // no longer active - we should close it. context.close(); } else { log.debug("Leaving transactional context open"); } }