Jasin Colegrove
12/09/2021, 11:42 AMRob Elliot
12/09/2021, 11:47 AMHikariDataSource
at application startup:
https://github.com/brettwooldridge/HikariCP#rocket-initialization
Pass that single instance as a constructor parameter (with type javax.sql.DataSource
) to the DAOs.
Then just use dataSource.getConnection().use { connection -> TODO("your code using the connection here") }
inside the DAO - it will reuse a pooled connection as far as possible.Rob Elliot
12/09/2021, 11:48 AMjava.sql.Connection
inside your DAOs? And are you wiring up your application by hand?)Rob Elliot
12/09/2021, 11:56 AMval dataSource = HikariDataSource(HikariConfig().apply {
initializationFailTimeout = -1
connectionTimeout = Duration.ofSeconds(1).toMillis()
})
then it will not block on startup or blowup if the DB is inaccessible, only when you try to get a connection will you get an exception after 1 second. So your app will be resilient to the DB being there or not. The default connection timeout of 30 seconds is a bit long!Jasin Colegrove
12/09/2021, 10:10 PMRob Elliot
12/09/2021, 10:16 PMval database = Database.connect(HikariDataSource(HikariConfig())
and then pass in database
to your DAO classes.Rob Elliot
12/09/2021, 10:21 PMJasin Colegrove
12/09/2021, 10:39 PM