r2dbc / r2dbc-pool

Connection Pooling for Reactive Relational Database Connectivity
https://r2dbc.io
Apache License 2.0
332 stars 55 forks source link

R2dbc pool with R2dbcEntityTemplate? #114

Closed thaibt closed 3 years ago

thaibt commented 3 years ago

Hello,

New to r2dbc, and coming originally hikari and jdbc. I was trying to set up pooling with R2dbc. I read https://github.com/r2dbc/r2dbc-pool and https://docs.spring.io/spring-data/r2dbc/docs/1.2.6/reference/html/#r2dbc.core. I was wondering how would I set up pooling with R2dbcEntityTemplate? Seems like they both param ConnectionFactory, and when I built the project - got this error

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'io.r2dbc.spi.ConnectionFactory' available: expected single matching bean but found 2: getConnectionFactory,getConnectionPool

Did I miss something from the documentation?

@Configuration
class ConnectionFactoryDao {
    @Bean
    fun getConnectionFactory(): ConnectionFactory {
        var options: ConnectionFactoryOptions? = ConnectionFactoryOptions.builder()
            .option(DRIVER, "pool")
            .option(PROTOCOL, "mysql")
            .option(HOST, "127.0.0.1")
            .option(USER, "root")
            .option(PORT, 3306) // optional, default 3306
            .option(PASSWORD, "database-password-in-here") // optional, default null, null means has no password
            .option(DATABASE, "r2dbc") // optional, default null, null means not specifying the database
            .option(CONNECT_TIMEOUT, Duration.ofSeconds(3)) // optional, default null, null means no timeout
            .option(SSL, true) // optional, default sslMode is "preferred", it will be ignore if sslMode is set
            .option(Option.valueOf("sslMode"), "verify_identity") // optional, default "preferred"
            .build()
        return ConnectionFactories.get(options!!)
    }

    @Bean
    fun getConnectionPool(@Autowired connectionFactory: ConnectionFactory): ConnectionPool {
        val configuration = ConnectionPoolConfiguration.builder(connectionFactory)
            .maxIdleTime(Duration.ofMillis(1000))
            .maxSize(20)
            .build()
        val pool = ConnectionPool(configuration)
        return pool
    }

    @Bean
    fun getR2dbcEntityTemplate(@Autowired connectionFactory: ConnectionFactory): R2dbcEntityTemplate {
        val template = R2dbcEntityTemplate(connectionFactory)
        return template
    }
}
thaibt commented 3 years ago

Whoops, sorry. Just an idiot. I didnt notice ConnectionPool implements ConnectionFactory