Thursday, 12 September 2013

Finally dispose of connection

Finally dispose of connection

I am connecting to an IBM Websphere MQ server in my .Net code and I wanted
to make sure that I am following best practice when using "finally".
I currently have the below code block which I believe can be modified to
just have the close portion in the finally clause. Is that correct? (I am
catching errors in the calling portion of the application).
try
{
var queueDepth = qmgr.AccessQueue(userQueue,
MQC.MQOO_INPUT_AS_Q_DEF +
MQC.MQOO_FAIL_IF_QUIESCING +
MQC.MQOO_INQUIRE).CurrentDepth;
if (qmgr.IsOpen)
qmgr.Close();
return queueDepth;
}
finally
{
if (qmgr.IsOpen)
qmgr.Close();
}
Is now this
try
{
var queueDepth = qmgr.AccessQueue(userQueue,
MQC.MQOO_INPUT_AS_Q_DEF +
MQC.MQOO_FAIL_IF_QUIESCING +
MQC.MQOO_INQUIRE).CurrentDepth;
return queueDepth;
}
finally
{
if (qmgr.IsOpen)
qmgr.Close();
}

No comments:

Post a Comment