Class SingleConnectionFactory

All Implemented Interfaces:
ConnectionFactory, ExceptionListener, QueueConnectionFactory, TopicConnectionFactory, DisposableBean, InitializingBean
Direct Known Subclasses:

A JMS ConnectionFactory adapter that returns the same Connection from all createConnection() calls, and ignores calls to Connection.close(). According to the JMS Connection model, this is perfectly thread-safe (in contrast to e.g. JDBC). The shared Connection can be automatically recovered in case of an Exception.

You can either pass in a specific JMS Connection directly or let this factory lazily create a Connection via a given target ConnectionFactory. This factory generally works with JMS 1.1 as well as the JMS 1.0.2 API.

Note that when using the JMS 1.0.2 API, this ConnectionFactory will switch into queue/topic mode according to the JMS API methods used at runtime: createQueueConnection and createTopicConnection will lead to queue/topic mode, respectively; generic createConnection calls will lead to a JMS 1.1 connection which is able to serve both modes.

As of Spring Framework 5, this class supports 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.

Useful for testing and standalone environments in order to keep using the same Connection for multiple JmsTemplate calls, without having a pooling ConnectionFactory underneath. This may span any number of transactions, even concurrently executing transactions.

Note that Spring's message listener containers support the use of a shared Connection within each listener container instance. Using SingleConnectionFactory in combination only really makes sense for sharing a single JMS Connection across multiple listener containers.

Juergen Hoeller, Mark Pollack
See Also:
  • Field Details

    • logger

      protected final Log logger
  • Constructor Details

    • SingleConnectionFactory

      public SingleConnectionFactory()
      Create a new SingleConnectionFactory for bean-style usage.
      See Also:
    • SingleConnectionFactory

      public SingleConnectionFactory(Connection targetConnection)
      Create a new SingleConnectionFactory that always returns the given Connection.
      targetConnection - the single Connection
    • SingleConnectionFactory

      public SingleConnectionFactory(ConnectionFactory targetConnectionFactory)
      Create a new SingleConnectionFactory that always returns a single Connection that it will lazily create via the given target ConnectionFactory.
      targetConnectionFactory - the target ConnectionFactory
  • Method Details