Search code examples
javaibm-mqconnection-leaks

MQ right way to close connection


I saw lots of examples like http://hursleyonwmq.wordpress.com/2007/05/29/simplest-sample-applications-using-websphere-mq-jms/, even on IBM publib. As I guess this code had a flaw: Queue connection closed in main block, and not in finally as I expected.

What is the right way to close MQ connection without leaks?


Solution

  • I think it is better to do it in finally. i.e.

    finally
    {
       try
       {
          session.close();
       }
       catch (Exception ex)
       {
          System.err.println("session.close() : " + ex.getLocalizedMessage());
       }
    
       try
       {
          connection.close();
       }
       catch (Exception ex)
       {
          System.err.println("connection.close() : " + ex.getLocalizedMessage());
       }
    }