Tidying up

 

When using JDBC to access a database it is the best practice to make sure that all objects are closed once finished with, including if an exception occurs. The same applies when using JMS.

Using some of the JMS objects can involve using objects from a pool as well as taking up JVM resource. The quicker the object is closed, the quicker it can be tidied up and resources re-used. For this reason it is recommended that any object with a close method should have close run on it as soon as it is no longer needed. Do not rely on just closing top level objects, like sessions or connections.

It is also important that the close method of the JMS objects occurs even if there has been an exception thrown. Figure 15-3 shows an example of how the finally block should be coded.

finally 
{
    //    Close QueueSender
    try 
    {
        if (sender != null) sender.close();
    } 
    catch (Exception e) 
    {
    }
    //    Close QueueSession
    try 
    {
        if (qs != null) qs.close();
    } 
    catch (Exception e) 
    {
    }
    //    Close QueueConnection
    try 
    {
        if (qc != null) qc.close();
    } 
    catch (Exception e) 
    {
    }
}

 Prev | Next

 

WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.

 

IBM is a trademark of the IBM Corporation in the United States, other countries, or both.