title: JmsTemplate and CachingConnectionFactory
body: <p>I'm finding some difficulties in trying to reduce the connection and session creation when sending messages to a Jboss Queue.</p>
  
  <pre><code>&lt;bean name="jbmTemplate-${jbmQueue}" class="org.springframework.jms.core.JmsTemplate" lazy-init="true"&gt;
          &lt;property name="sessionTransacted" value="true" /&gt;
  
      &lt;!-- connection factory --&gt;
      &lt;property name="connectionFactory"&gt;
          &lt;ref bean="jbmCachingConnectionFactory"/&gt;
      &lt;/property&gt;
  
      &lt;property name="pubSubDomain"&gt;
          &lt;value&gt;false&lt;/value&gt;
      &lt;/property&gt;
  
      &lt;property name="receiveTimeout"&gt;
          &lt;value&gt;${jmsReceiveTimeout}&lt;/value&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  </code></pre>
  
  <p>I'm using a JmsTemplate to send messages:</p>
  
  <pre><code>&lt;bean id="jbmCachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"&gt;
       &lt;property name="targetConnectionFactory" ref="jbmConnectionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>And the jbmConnectionFactory is defined as follow:</p>
  
  <pre><code>&lt;jee:jndi-lookup id="jbmConnectionFactory" jndi-name="XAConnectionFactory"
          resource-ref="true" lookup-on-startup="false" proxy-interface="javax.jms.ConnectionFactory"
          cache="false"&gt;
      &lt;jee:environment&gt;
          java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
          java.naming.provider.url=${jbmURL}
          java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
      &lt;/jee:environment&gt;
  &lt;/jee:jndi-lookup&gt;
  </code></pre>
  
  <p>I'm using this piece of code to send messages to Jboss:</p>
  
  <pre><code>public void send(final Serializable payload, final String correlationId) {
      log.debug("payload: {}", payload);
  
      try {
          jmsQueueTemplate.send(destinationName, new MessageCreator() {
              public Message createMessage(Session session)
                      throws JMSException {
                  ObjectMessage msg = session.createObjectMessage(payload);
  
                  msg.setJMSType(payload.getClass().getCanonicalName());
                  msg.setJMSCorrelationID(correlationId);
  
                  return msg;
              }
  
          });
      } catch (Exception e) {
          log.error("Couldn't send message");
          throw new BlockingException(e);
      }
  }
  </code></pre>
  
  <p>What I was trying to do by using the CachingConnectionFactory is to avoid creating and closing connections and sessions every message I send but as far as I can see, when using a JmsTemplate, no matter what type of connectionFactory I use, the connection and session will always be terminated:</p>
  
  <pre><code>finally {
              JmsUtils.closeSession(sessionToClose);
              ConnectionFactoryUtils.releaseConnection(conToClose, getConnectionFactory(), startConnection);
          }
  </code></pre>
  
  <p>Am I calling the wrong method? Is there something that I don't get?</p>