Added exception handling and bugsense logging for database errors