r2dbc / r2dbc-pool

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

Update documentation with correct pool construction #121

Closed petromir closed 3 years ago

petromir commented 3 years ago

@mp911de During investigation of stale idle connections in reactor-pool, Simon Baslé noted an interesting problem regarding ConnectionFactory and ConnectionPool construction (https://github.com/reactor/reactor-pool/issues/136#issuecomment-834263342). In my opinion the documentation should be updated, so when people copy paste the examples they won't accidentally create 2 pools instead of one, as this results in 10 always open connections.

mp911de commented 3 years ago

You mean configuring ConnectionPool with ConnectionFactories.get(ConnectionFactoryOptions.builder().option(DRIVER, "pool").option(PROTOCOL, "postgresql")…)?

Do you have a suggestion? ConnectionFactories.get(…) is part of the SPI and can only return ConnectionFactory and not ConnectionPool.

petromir commented 3 years ago

Suggested solution could be found here https://github.com/petromir/r2dbc-pool-idle-connection-issue/blob/master/src/main/java/com/example/demo/R2dbcPostgreSQLConfig.java#L45 I'm still not sure how this will work when the user wants to use a proxy, but I will have to try it soon as this what we are using in my regular job.

mp911de commented 3 years ago

But isn't that basically what we already have:

Programmatic Configuration

ConnectionFactory connectionFactory = …;

ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
   .maxIdleTime(Duration.ofMillis(1000))
   .maxSize(20)
   .build();

ConnectionPool pool = new ConnectionPool(configuration);

Mono<Connection> connectionMono = pool.create();

// later

Connection connection = …;
Mono<Void> release = connection.close(); // released the connection back to the pool

// application shutdown
pool.dispose();

I think we can add a bit of comment to folks willing to read the docs to outline what connectionFactory actually represents.

petromir commented 3 years ago

Thanks!