11.4.4 Freeing JMS object resources

 

+

Search Tips   |   Advanced Search

 

When using JDBC to access a database it is a best practice to make sure that all objects are closed after their use - even when exceptions occurs. The same applies when using JMS. Figure 11-3 shows an example of how the finally block after a try-catch should be coded after a JMS connection is no longer needed.

The close method for javax.jms.Connection triggers a close for all JMS objects below it in the hierarchy. For example, closing a QueueConnection (a subinterface of Connection) will close a QueueSender or QueueSession created under that connection. Closing the highest object in the JMS hierarchy which is no longer needed is recommended.

finally 
{
  //  Close JMS Connection
  try 
  {
    if (connection != null) 
    {
      connection.close();
      connection = null;
    }
  } 
  catch (JMSException jmse) 
  {
    logger.error("Connection close() failed within MyServlet", jmse);
    if(jmse.getLinkedException() != null)
      logger.error("linked cause for error", jmse.getLinkedException());
  }
}

Next