From: Olaf Wintermann Date: Fri, 29 Aug 2025 18:15:41 +0000 (+0200) Subject: remove connection field from Database and always use the DataSource X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=6bb65a323187f9627ff6b9e6732b84d2d3fe0835;p=rssreader.git remove connection field from Database and always use the DataSource --- 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 ->