From 6bb65a323187f9627ff6b9e6732b84d2d3fe0835 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Fri, 29 Aug 2025 20:15:41 +0200 Subject: [PATCH] remove connection field from Database and always use the DataSource --- .../main/kotlin/de/unixwork/rssreader/Database.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt index 214e16d..d67db36 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt @@ -12,18 +12,15 @@ import java.sql.DriverManager import java.sql.Statement object Database { - val connection: Connection val dataSource: HikariDataSource init { - connection = DriverManager.getConnection("jdbc:h2:~/.rssreader/feeds") - val config = HikariConfig() config.jdbcUrl = "jdbc:h2:~/.rssreader/feeds" config.maximumPoolSize = 16 dataSource = HikariDataSource(config) - ensureSchema(connection) + ensureSchema(dataSource.connection) } private fun ensureSchema(conn: Connection) { @@ -97,7 +94,7 @@ object Database { public fun getFeedTree(context: Context) : MutableList { val groups = mutableListOf() - connection.createStatement().use { stmt -> + dataSource.connection.createStatement().use { stmt -> val rs = stmt.executeQuery(""" select g.group_id, @@ -134,7 +131,7 @@ object Database { public fun newFeedGroup(context: Context, name: String) : FeedGroup { var groupId = 0 - connection.prepareStatement(""" + dataSource.connection.prepareStatement(""" insert into groups (pos, name) select coalesce(max(pos), 0)+1, ? from groups """.trimIndent(), Statement.RETURN_GENERATED_KEYS).use { stmt -> stmt.setString(1, name) @@ -159,6 +156,7 @@ object Database { cert: String? = null) : FeedCollection { var feedcollectionId = -1 + val connection = dataSource.connection connection.prepareStatement(""" insert into feedcollections (group_id, pos, name) select ?, coalesce(max(pos), 0)+1, ? from groups """.trimIndent(), Statement.RETURN_GENERATED_KEYS).use { stmt -> @@ -197,7 +195,7 @@ object Database { public fun getItems(feedCollection: FeedCollection, maxItems: Int) : MutableList { val items = mutableListOf() - connection.prepareStatement(""" + dataSource.connection.prepareStatement(""" select I.*, F.URL from items I inner join feeds F on I.feed_id = F.feed_id where F.feedcollection_id = ? order by pub_date desc limit ? @@ -228,7 +226,7 @@ object Database { public fun getAllFeeds() : MutableList { val feeds = mutableListOf() - connection.prepareStatement(""" + dataSource.connection.prepareStatement(""" select * from feeds """.trimIndent()).use { stmt -> stmt.executeQuery().use { rs -> -- 2.47.3