Min Thet Naing
10/18/2025, 5:24 AMinterface DBConnectionFactory {
fun connect(dbConfig: DbConfig, ktorEnvironment: KtorEnvironment): R2dbcDatabase
}
object PostgresConnectionFactory: DBConnectionFactory {
override fun connect(dbConfig: DbConfig, ktorEnvironment: KtorEnvironment): R2dbcDatabase {
val connectionFactory = ConnectionFactories.get(
builder()
.option(DRIVER, "pool")
.option(PROTOCOL, "postgresql")
.option(HOST, dbConfig.host)
.option(PORT, dbConfig.port) // default
.option(USER, dbConfig.user)
.option(PASSWORD, dbConfig.password)
.option(DATABASE, dbConfig.dbName)
.apply {
if (ktorEnvironment == KtorEnvironment.Prod) {
option(SSL, true)
option(SSL_MODE, SSLMode.REQUIRE)
}
}
.option(PoolingConnectionFactoryProvider.MAX_IDLE_TIME, Duration.ofMillis(5000))
.option(PoolingConnectionFactoryProvider.INITIAL_SIZE, 5)
.option(PoolingConnectionFactoryProvider.MAX_SIZE, 10) // pool size
.build())
val db = R2dbcDatabase(
connectionFactory = connectionFactory,
dialect = PostgreSqlR2dbcDialect()
)
return db
}
}
When the Ktor server shutdown do I need to manually close the r2dbc-pool connection?
If I need to manually close the pool connection, how can I do this?
Please show me the example and thank you.Toshihiro Nakamura
10/18/2025, 9:09 AMRuntime.getRuntime().addShutdownHook(Thread {
val db: R2dbcDatabase = ...
val connectionFactory = db.config.session.connectionFactory
if (connectionFactory is io.r2dbc.pool.ConnectionPool) {
connectionFactory.dispose()
}
})Min Thet Naing
10/18/2025, 9:17 AMinterface DBConnectionFactory {
fun connect(dbConfig: DbConfig, ktorEnvironment: KtorEnvironment): R2dbcDatabase
}
object PostgresConnectionFactory: DBConnectionFactory, AutoCloseable {
private var db: R2dbcDatabase? = null
override fun connect(dbConfig: DbConfig, ktorEnvironment: KtorEnvironment): R2dbcDatabase {
val connectionFactory = ConnectionFactories.get(
builder()
.option(DRIVER, "pool")
.option(PROTOCOL, "postgresql")
.option(HOST, dbConfig.host)
.option(PORT, dbConfig.port) // default
.option(USER, dbConfig.user)
.option(PASSWORD, dbConfig.password)
.option(DATABASE, dbConfig.dbName)
.apply {
if (ktorEnvironment == KtorEnvironment.Prod) {
option(SSL, true)
option(SSL_MODE, SSLMode.REQUIRE)
}
}
.option(PoolingConnectionFactoryProvider.MAX_IDLE_TIME, Duration.ofMillis(5000))
.option(PoolingConnectionFactoryProvider.INITIAL_SIZE, 5)
.option(PoolingConnectionFactoryProvider.MAX_SIZE, 10) // pool size
.build())
db = R2dbcDatabase(
connectionFactory = connectionFactory,
dialect = PostgreSqlR2dbcDialect()
)
return db!!
}
override fun close() {
val connectionFactory = db?.config?.session?.connectionFactory
if (connectionFactory is ConnectionPool) {
connectionFactory.dispose()
}
}
}
I use io.ktor:ktor-server-di plugins
Is it ok?Toshihiro Nakamura
10/18/2025, 12:09 PMo.ktor:ktor-server-di, it seems better to manage R2dbcDatabase rather than PostgresConnectionFactory.
In that case, you can write it like this:
object PostgresConnectionFactory: DBConnectionFactory, AutoCloseable {
override fun connect(dbConfig: DbConfig, ktorEnvironment: KtorEnvironment): R2dbcDatabase {
val connectionFactory = ConnectionFactories.get(
builder()
.option(DRIVER, "pool")
.option(PROTOCOL, "postgresql")
.option(HOST, dbConfig.host)
.option(PORT, dbConfig.port) // default
.option(USER, dbConfig.user)
.option(PASSWORD, dbConfig.password)
.option(DATABASE, dbConfig.dbName)
.apply {
if (ktorEnvironment == KtorEnvironment.Prod) {
option(SSL, true)
option(SSL_MODE, SSLMode.REQUIRE)
}
}
.option(PoolingConnectionFactoryProvider.MAX_IDLE_TIME, Duration.ofMillis(5000))
.option(PoolingConnectionFactoryProvider.INITIAL_SIZE, 5)
.option(PoolingConnectionFactoryProvider.MAX_SIZE, 10) // pool size
.build())
val db = R2dbcDatabase(
connectionFactory = connectionFactory,
dialect = PostgreSqlR2dbcDialect()
)
return object : R2dbcDatabase by db, AutoCloseable {
override fun close() {
val connectionFactory = config.session.connectionFactory
if (connectionFactory is ConnectionPool) {
connectionFactory.dispose()
}
}
}
}
}