database: Add checks for closed DB

Signed-off-by: Michael Buesch <m@bues.ch>