Now closing the database when errors occur. Previously this could have hung the process on shutdown if an error had occurred.