From f434482cdf5bd1889813511cbe2571820f8343cd Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 12 Apr 2018 05:55:45 +0200 Subject: [PATCH] Database connection will now always be closed on VM shutdown. --- .../fabianonline/telegram_backup/CommandLineController.kt | 4 ++++ .../kotlin/de/fabianonline/telegram_backup/Database.kt | 7 +++++++ 2 files changed, 11 insertions(+) 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 {