dave08
11/21/2023, 11:55 AMCan't create more than max_prepared_stmt_count statements...
after migrating to Komapper from Jasync? (It could be that it's not Komapper... I'm just double checking, or maybe someone might know another reason for it?)Toshihiro Nakamura
11/21/2023, 1:23 PMdave08
11/21/2023, 1:27 PMdave08
11/21/2023, 1:28 PMio.r2dbc:r2dbc-pool
dave08
11/21/2023, 1:29 PMdata class ConnectionPoolConfig (
val name: String? = null,
val aquireRetry: Int = 1,
val backgroundEvictionInterval: Duration = ConnectionPoolConfiguration.NO_TIMEOUT,
val initialSize: Int? = null,
val maxSize: Int? = null,
val minIdle: Int = 0,
val maxAquireTime: Duration = ConnectionPoolConfiguration.NO_TIMEOUT,
val maxCreateConnectionTime: Duration = ConnectionPoolConfiguration.NO_TIMEOUT,
val maxIdleTime: Duration = Duration.ofMinutes(30),
val maxLifeTime: Duration = ConnectionPoolConfiguration.NO_TIMEOUT,
val maxValidationTime: Duration = ConnectionPoolConfiguration.NO_TIMEOUT,
val validationQuery: String? = null,
val validationDepth: ValidationDepth = ValidationDepth.LOCAL,
)
interface DatabaseModule {
@Provides @Singleton
fun provideDatabase(configs: DatabaseSettings2): R2dbcDatabase {
val db = configs
val cp = configs.poolSettings
val poolConfig = ConnectionPoolConfiguration.builder().apply {
cp.name?.let { name(it) }
acquireRetry(cp.aquireRetry)
backgroundEvictionInterval(cp.backgroundEvictionInterval)
cp.initialSize?.let { initialSize(it) }
cp.maxSize?.let { maxSize(it) }
minIdle(cp.minIdle)
maxAcquireTime(cp.maxAquireTime)
maxCreateConnectionTime(cp.maxCreateConnectionTime)
maxIdleTime(cp.maxIdleTime)
maxLifeTime(cp.maxLifeTime)
maxValidationTime(cp.maxValidationTime)
cp.validationQuery?.let { validationQuery(it) }
validationDepth(cp.validationDepth)
}
val pool = ConnectionPool(
poolConfig.connectionFactory(
ConnectionFactories.get(
ConnectionFactoryOptions.builder()
.option(ConnectionFactoryOptions.DATABASE, db.dbName)
.option(ConnectionFactoryOptions.DRIVER, "mysql")
.option(ConnectionFactoryOptions.HOST, db.host)
.option(ConnectionFactoryOptions.PORT, db.port)
.option(ConnectionFactoryOptions.USER, db.username)
.option(ConnectionFactoryOptions.PASSWORD, db.password)
.build()
)
)
.build()
)
all with default values so far...dave08
11/21/2023, 1:31 PMToshihiro Nakamura
11/21/2023, 1:44 PMdave08
11/21/2023, 1:51 PMdave08
11/21/2023, 1:55 PMkomapper-dialect-mysql-r2dbc = { module = "org.komapper:komapper-dialect-mysql-r2dbc" }
which bring in:dave08
11/21/2023, 1:56 PMdave08
11/21/2023, 2:05 PMdave08
11/21/2023, 2:42 PMToshihiro Nakamura
11/21/2023, 9:06 PMjst.releasePreparedStatementAfterUse
function as follows:
val db = R2dbcDatabase("r2dbc:h2:mem:///example;DB_CLOSE_DELAY=-1")
val newConfig = object : R2dbcDatabaseConfig by db.config {
val delegatee: R2dbcSession = db.config.session
override val session: R2dbcSession = object : R2dbcSession {
override val connectionFactory: ConnectionFactory
get() = delegatee.connectionFactory
override val coroutineTransactionOperator: CoroutineTransactionOperator
get() = delegatee.coroutineTransactionOperator
override val flowTransactionOperator: FlowTransactionOperator
get() = delegatee.flowTransactionOperator
override suspend fun getConnection(): Connection {
val connection = delegatee.getConnection()
return object : Connection by connection {
override fun createStatement(sql: String): Statement {
return connection.createStatement(sql).also {
//if (it is JasyncStatement) it.releasePreparedStatementAfterUse()
}
}
}
}
override suspend fun releaseConnection(connection: Connection) {
delegatee.releaseConnection(connection)
}
}
}
// In the subsequent processes, use this object.
val newDb = R2dbcDatabase(newConfig)
dave08
11/22/2023, 11:58 AMToshihiro Nakamura
11/22/2023, 9:55 PMreleasePreparedStatementAfterUse
function is a unique API to Jasync, so incorporating it into Komapper might be a bit hesitant. How about creating an extension function like the following in your application?
fun R2dbcDatabase.releasePreparedStatementAfterUse(): R2dbcDatabase {
// use the code described above
return ...
}
You can use the above extension function as follows:
db.releasePreparedStatementAfterUse().runQuery(query)
This approach allows you to integrate the specific functionality of Jasync into Komapper in a way that’s isolated to your application, without impacting the core of Komapper.dave08
11/23/2023, 10:34 AMToshihiro Nakamura
11/23/2023, 11:42 AMdave08
11/23/2023, 11:44 AMdave08
11/23/2023, 11:45 AMToshihiro Nakamura
11/23/2023, 1:07 PMdave08
11/28/2023, 1:18 PMinternal final class JasyncStatement
Toshihiro Nakamura
11/29/2023, 1:28 PMdave08
11/29/2023, 1:29 PM