Added error handling for lost connections and locked database.