Fixed closing database connection on error