Close database when HistorySavingThread stops