title: Closing Session when using Spring's CachingConnectionFactory
body: <p>The java doc <a href="http://docs.spring.io/spring/docs/3.2.4.RELEASE/javadoc-api/" rel="noreferrer">here</a> related to Spring CachingConnectionFactory has comment :</p>
  
  <blockquote>
    <p>NOTE: This ConnectionFactory requires explicit closing of all Sessions obtained from its shared Connection. This is the usual recommendation for native JMS access code anyway. However, with this ConnectionFactory, its use is mandatory in order to actually allow for Session reuse.</p>
  </blockquote>
  
  <p>I am not clear how to handle this with the below given configuration in my application. </p>
  
  <pre><code>&lt;bean id="springApp" class="com.codereq.springcore.jms.SpringJMSListenerApp"  /&gt;
  
  &lt;bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"&gt;
      &lt;property name="connectionFactory" ref="jmsConnectionFactory"/&gt;
      &lt;property name="destination" ref="destination"/&gt;
      &lt;property name="messageListener" ref="messageListener"/&gt;
      &lt;property name="sessionTransacted" value="true"/&gt;
      &lt;property name="concurrentConsumers" value="5" /&gt;
      &lt;property name="maxConcurrentConsumers" value="15" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="messageListener" class="com.codereq.springcore.jms.MessageListenerApp" /&gt;
  
  &lt;bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"
          p:targetConnectionFactory-ref="emsConnectionFactory"
          p:sessionCacheSize="100" 
          p:cacheConsumers="true" /&gt;
  
  &lt;bean id="emsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName" value="GenericConnectionFactory"/&gt;
      &lt;property name="jndiTemplate" ref="jndiTemplate"/&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"&gt;
      &lt;property name="environment"&gt;
          &lt;props&gt;
              &lt;prop key="java.naming.factory.initial"&gt;com.tibco.tibjms.naming.TibjmsInitialContextFactory&lt;/prop&gt;
              &lt;prop key="java.naming.provider.url"&gt;tibjmsnaming://localhost:7222&lt;/prop&gt;
              &lt;prop key="java.naming.security.principal"&gt;admin&lt;/prop&gt;
              &lt;prop key="java.naming.security.credentials"&gt;admin&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="destination" class="com.tibco.tibjms.TibjmsQueue"&gt;
      &lt;constructor-arg value="com.sample.queue" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The listener class is this :</p>
  
  <pre><code>public class MessageListenerApp implements MessageListener {
  
  private static int c = 0;
  
  @Override
  public void onMessage(Message arg0) {
  
      try {
          System.out.println("Received Message..."+arg0.getStringProperty("MessageNum")+". Waiting to finish..");
          Thread.sleep(2000);
          System.out.println("Finished processing.."+arg0.getStringProperty("MessageNum")+".."+(c++));
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>How do I follow recommendation that Sessions obtained from shared connection should be closed explicitly ?</p>
  
  <p>Came across SessionAwareMessageListener interface which provides onMessage method which gives handle to Session. So to properly implement session closing, should this interface be implemented ?</p>