Why connection.close() not closing out the MQ connection?

718 Views Asked by At

I'm using the connection.start() to start the connection and consumer.receive() to receive the messages from the queue. But while closing the connection, it's not able to close the connection using connection.close(). Due to this it's exhausting the connection limit and throwing an exception that queue manager is not available.

What's the reason behind this? and how to solve it?

    connectionWMQ = connectionFactory.CreateConnection();
    connectionWMQ.ExceptionListener = new ExceptionListener(OnXMSException);

    // Create session
    ISession sessionWMQ = connectionWMQ.CreateSession(false, AcknowledgeMode.AutoAcknowledge);

    IDestination destination = sessionWMQ.CreateQueue("QueueName");
    IMessageConsumer consumer=sessionWMQ.CreateConsumer(destination);
    try{
         connectionWMQ.Start();
         var message=(IMessage)Consumer.Receive(TIMEOUTTIME);
         //decoding the msg;

         connectionWMQ.Close();
       }
  catch(Exception ex){
       }
1

There are 1 best solutions below

0
On

After a successful completion of connectionFactory.CreateConnection() you need to make sure to close the connection finally. The code you provide does not guarantee this, there are some calls which could fail, with the result it would skip closing the connection. You could for example move everything behind CreateConnection() into a try block and move close() call to the corresponding finally block.