Improve error handling when database connection is lost.
More improvements to error handling when database connection is lost.