I'm trying to configure R2DBC connection to PostgreSQL database with connection pool. I have followed this documentation: https://github.com/r2dbc/r2dbc-pool/blob/main/README.md.
ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
.option(DRIVER, "pool")
.option(PROTOCOL, "postgresql") // driver identifier, PROTOCOL is delegated as DRIVER by the pool.
.option(HOST, "192.168.1.200")
.option(PORT, 5433)
.option(DATABASE, "XXX")
.option(USER, "XXX")
.option(PASSWORD, "XXX")
.build());
ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
.maxIdleTime(Duration.ofMillis(1000))
.initialSize(10)
.maxSize(100)
.build();
ConnectionPool pool = new ConnectionPool(configuration);
Mono<Connection> connection = pool.create();
return Flux.from(connection)
.flatMap(conn -> conn
.createStatement("SELECT id, name, description FROM assortment")
.execute())
.flatMap(result -> result
.map((row, rowMetadata) ->
new AssortmentItem(
row.get("id", Long.class),
row.get("name", String.class),
row.get("description", String.class))
));
But I don't know how to close connection after query, to get it back to pool. I've tried:
connection.close();
but it doesn't work, try-with-resources to. Without this after 10 calls I got "io.r2dbc.postgresql.ExceptionFactory$PostgresqlNonTransientResourceException" with message "sorry, too many clients already".
How to close connection and get it back to pool in my example? Why I can only call 10 queries from pool (initial pool size) when max pool size is 100?