org.springframework.jms.connection

## Class TransactionAwareConnectionFactoryProxy

- java.lang.Object
- - org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy

- All Implemented Interfaces:
    ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory

      public class TransactionAwareConnectionFactoryProxy
      extends Object
      implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory

  Proxy for a target JMS `ConnectionFactory`, adding awareness of Spring-managed transactions. Similar to a transactional JNDI ConnectionFactory as provided by a Java EE application server.

  Messaging code which should remain unaware of Spring's JMS support can work with this proxy to seamlessly participate in Spring-managed transactions. Note that the transaction manager, for example `JmsTransactionManager`, still needs to work with the underlying ConnectionFactory, not with this proxy.

  Make sure that TransactionAwareConnectionFactoryProxy is the outermost ConnectionFactory of a chain of ConnectionFactory proxies/adapters. TransactionAwareConnectionFactoryProxy can delegate either directly to the target factory or to some intermediary adapter like `UserCredentialsConnectionFactoryAdapter`.

  Delegates to `ConnectionFactoryUtils` for automatically participating in thread-bound transactions, for example managed by `JmsTransactionManager`. `createSession` calls and `close` calls on returned Sessions will behave properly within a transaction, that is, always work on the transactional Session. If not within a transaction, normal ConnectionFactory behavior applies.

  Note that transactional JMS Sessions will be registered on a per-Connection basis. To share the same JMS Session across a transaction, make sure that you operate on the same JMS Connection handle - either through reusing the handle or through configuring a `SingleConnectionFactory` underneath.

  Returned transactional Session proxies will implement the `SessionProxy` interface to allow for access to the underlying target Session. This is only intended for accessing vendor-specific Session API or for testing purposes (e.g. to perform manual transaction control). For typical application purposes, simply use the standard JMS Session interface.

  As of Spring Framework 5, this class delegates JMS 2.0 `JMSContext` calls and therefore requires the JMS 2.0 API to be present at runtime. It may nevertheless run against a JMS 1.1 driver (bound to the JMS 2.0 API) as long as no actual JMS 2.0 calls are triggered by the application's setup.

  Since:
    2.0
  Author:
    Juergen Hoeller
  See Also:
    `UserCredentialsConnectionFactoryAdapter`, `SingleConnectionFactory`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `TransactionAwareConnectionFactoryProxy()` Create a new TransactionAwareConnectionFactoryProxy.
    `TransactionAwareConnectionFactoryProxy(ConnectionFactory targetConnectionFactory)` Create a new TransactionAwareConnectionFactoryProxy.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Connection` | `createConnection()`
    `Connection` | `createConnection(String username, String password)`
    `JMSContext` | `createContext()`
    `JMSContext` | `createContext(int sessionMode)`
    `JMSContext` | `createContext(String userName, String password)`
    `JMSContext` | `createContext(String userName, String password, int sessionMode)`
    `QueueConnection` | `createQueueConnection()`
    `QueueConnection` | `createQueueConnection(String username, String password)`
    `TopicConnection` | `createTopicConnection()`
    `TopicConnection` | `createTopicConnection(String username, String password)`
    `protected ConnectionFactory` | `getTargetConnectionFactory()` Return the target ConnectionFactory that this ConnectionFactory should delegate to.
    `protected Connection` | `getTransactionAwareConnectionProxy(Connection target)` Wrap the given Connection with a proxy that delegates every method call to it but handles Session lookup in a transaction-aware fashion.
    `protected boolean` | `isSynchedLocalTransactionAllowed()` Return whether to allow for a local JMS transaction that is synchronized with a Spring-managed transaction.
    `void` | `setSynchedLocalTransactionAllowed(boolean synchedLocalTransactionAllowed)` Set whether to allow for a local JMS transaction that is synchronized with a Spring-managed transaction (where the main transaction might be a JDBC-based one for a specific DataSource, for example), with the JMS transaction committing right after the main transaction.
    `void` | `setTargetConnectionFactory(ConnectionFactory targetConnectionFactory)` Set the target ConnectionFactory that this ConnectionFactory should delegate to.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### TransactionAwareConnectionFactoryProxy

          public TransactionAwareConnectionFactoryProxy()

      Create a new TransactionAwareConnectionFactoryProxy.

    - #### TransactionAwareConnectionFactoryProxy

          public TransactionAwareConnectionFactoryProxy(ConnectionFactory targetConnectionFactory)

      Create a new TransactionAwareConnectionFactoryProxy.
      Parameters:
        `targetConnectionFactory` - the target ConnectionFactory

  - ### Method Detail

    - #### setTargetConnectionFactory

          public final void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory)

      Set the target ConnectionFactory that this ConnectionFactory should delegate to.

    - #### getTargetConnectionFactory

          protected ConnectionFactory getTargetConnectionFactory()

      Return the target ConnectionFactory that this ConnectionFactory should delegate to.

    - #### setSynchedLocalTransactionAllowed

          public void setSynchedLocalTransactionAllowed(boolean synchedLocalTransactionAllowed)

      Set whether to allow for a local JMS transaction that is synchronized with a Spring-managed transaction (where the main transaction might be a JDBC-based one for a specific DataSource, for example), with the JMS transaction committing right after the main transaction. If not allowed, the given ConnectionFactory needs to handle transaction enlistment underneath the covers.

      Default is "false": If not within a managed transaction that encompasses the underlying JMS ConnectionFactory, standard Sessions will be returned. Turn this flag on to allow participation in any Spring-managed transaction, with a local JMS transaction synchronized with the main transaction.

    - #### isSynchedLocalTransactionAllowed

          protected boolean isSynchedLocalTransactionAllowed()

      Return whether to allow for a local JMS transaction that is synchronized with a Spring-managed transaction.

    - #### createConnection

          public Connection createConnection()
                                      throws JMSException

      Specified by:
        `createConnection` in interface `ConnectionFactory`
      Throws:
        `JMSException`

    - #### createConnection

          public Connection createConnection(String username,
                                             String password)
                                      throws JMSException

      Specified by:
        `createConnection` in interface `ConnectionFactory`
      Throws:
        `JMSException`

    - #### createQueueConnection

          public QueueConnection createQueueConnection()
                                                throws JMSException

      Specified by:
        `createQueueConnection` in interface `QueueConnectionFactory`
      Throws:
        `JMSException`

    - #### createQueueConnection

          public QueueConnection createQueueConnection(String username,
                                                       String password)
                                                throws JMSException

      Specified by:
        `createQueueConnection` in interface `QueueConnectionFactory`
      Throws:
        `JMSException`

    - #### createTopicConnection

          public TopicConnection createTopicConnection()
                                                throws JMSException

      Specified by:
        `createTopicConnection` in interface `TopicConnectionFactory`
      Throws:
        `JMSException`

    - #### createTopicConnection

          public TopicConnection createTopicConnection(String username,
                                                       String password)
                                                throws JMSException

      Specified by:
        `createTopicConnection` in interface `TopicConnectionFactory`
      Throws:
        `JMSException`

    - #### createContext

          public JMSContext createContext()

      Specified by:
        `createContext` in interface `ConnectionFactory`

    - #### createContext

          public JMSContext createContext(String userName,
                                          String password)

      Specified by:
        `createContext` in interface `ConnectionFactory`

    - #### createContext

          public JMSContext createContext(String userName,
                                          String password,
                                          int sessionMode)

      Specified by:
        `createContext` in interface `ConnectionFactory`

    - #### createContext

          public JMSContext createContext(int sessionMode)

      Specified by:
        `createContext` in interface `ConnectionFactory`

    - #### getTransactionAwareConnectionProxy

          protected Connection getTransactionAwareConnectionProxy(Connection target)

      Wrap the given Connection with a proxy that delegates every method call to it but handles Session lookup in a transaction-aware fashion.
      Parameters:
        `target` - the original Connection to wrap
      Returns:
        the wrapped Connection