Fixed some database connnection close issues