diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt index 25df745..6b5715d 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt @@ -115,6 +115,10 @@ class CommandLineController(val options: CommandLineOptions) { // If we reach this point, we can assume that there is an account and a database can be loaded / created. database = Database(file_base, user_manager) + Runtime.getRuntime().addShutdownHook(Thread() { + database.close() + }) + // Load the settings and stuff. settings = Settings(file_base, database, options) diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt index 6959f18..8175952 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt @@ -662,6 +662,13 @@ class Database constructor(val file_base: String, val user_manager: UserManager) rs.close() return list } + + fun close() { + logger.debug("Closing database.") + try { stmt.close() } catch (e: Throwable) { logger.debug("Exception during stmt.close()", e) } + try { conn.close() } catch (e: Throwable) { logger.debug("Exception during conn.close()", e) } + logger.debug("Database closed.") + } abstract inner class AbstractChat {