Fixes to database connection handling