Fix crashing on exit sometimes due to DB not closed