MIRTH-2234
Variable declaration was moved to a global scope and a try/finally block was added to ensure that the database connection is always closed in case an error occurs.
An if statement was added in the finally clause to determine whether the database connection exists and needs to be closed.