Added extra check to make sure that database is open.