check if db was opened before closing it