```interface DBConnectionFactory { fun connect...
# komapper
m
Copy code
interface 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.
t
Try the following code:
Copy code
Runtime.getRuntime().addShutdownHook(Thread {
    val db: R2dbcDatabase = ...
    val connectionFactory = db.config.session.connectionFactory
    if (connectionFactory is io.r2dbc.pool.ConnectionPool) {
        connectionFactory.dispose()
    }
})
m
I try it like this and implement AutoClosable
Copy code
interface 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?
t
With
o.ktor:ktor-server-di
, it seems better to manage
R2dbcDatabase
rather than
PostgresConnectionFactory
. In that case, you can write it like this:
Copy code
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()
                }
            }

        }
    }
}