Fixed defensive programing around #19924

If the share is closed, then db will be None, so calling is_closed()
won't work.