Developer Playground
R2DBC connection pooling implements a similar approach to traditional JDBC connection pools like HikariCP, with some notable differences in connection handling philosophy.
The ConnectionPool
in R2DBC provides several configuration options for managing the lifecycle of database connections, including support for minimum idle connections.
ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
.initialSize(10)
.maxSize(10)
.minIdle(5)
.maxIdleTime(Duration.ofMinutes(30))
.build();
ConnectionPool pool = new ConnectionPool(configuration);
// R2DBC implementation code showing minIdle support
int cpuCount = Runtime.getRuntime().availableProcessors();
PoolBuilder<Connection, PoolConfig<Connection>> builder = PoolBuilder.from(allocator)
.clock(configuration.getClock())
.metricsRecorder(metricsRecorder)
.evictionPredicate(evictionPredicate)
.destroyHandler(Connection::close)
.sizeBetween(Math.min(configuration.getMinIdle(), cpuCount), cpuCount)
.idleResourceReuseMruOrder();
if (maxSize != -1 && initialSize <= 0) {
builder.sizeBetween(Math.max(configuration.getMinIdle(), initialSize), maxSize);
} else {
builder.sizeBetween(Math.max(configuration.getMinIdle(), initialSize),
maxSize == -1 ? Integer.MAX_VALUE : maxSize);
}
Understanding how R2DBC manages connection eviction is important for optimizing pool performance.
// Create eviction predicate that checks maxIdleTime and maxLifeTime.
// This is because "PoolBuilder#evictionIdle()" and "PoolBuilder#evictionPredicate()" cannot be used together in
// current implementation. (<https://github.com/reactor/reactor-pool/issues/33>)
// To workaround the issue, here defines an evictionPredicate that performs both maxIdleTime and maxLifeTime check.
BiPredicate<Connection, PooledRefMetadata> evictionPredicate = (connection, metadata) -> {
if (maxIdleTime.isZero() || maxLifeTime.isZero()) {
// evict immediately
return true;
}
boolean isIdleTimeExceeded = !maxIdleTime.isNegative() && metadata.idleTime() >= maxIdleTime.toMillis();
boolean isLifeTimeExceeded = !maxLifeTime.isNegative() && metadata.lifeTime() >= maxLifeTime.toMillis();
return isIdleTimeExceeded || isLifeTimeExceeded;
};
This code demonstrates how R2DBC implements the eviction predicate that determines when connections should be removed from the pool. The eviction occurs if either the idle time or lifetime thresholds are exceeded, while maintaining the minimum idle connections specified.
R2DBC's connection pool design balances reactive programming principles with practical connection management, providing both efficiency and availability.
Important Note: R2DBC's support for minimum idle connections ensures consistent availability with minimal latency, providing a similar guarantee to what traditional connection pools like HikariCP offer.
R2DBC connection pool combines a reactive design philosophy with practical connection management features similar to traditional JDBC connection pools like HikariCP. Key features include:
This document confirms that R2DBC does support a minimum idle connection feature similar to HikariCP's minimumIdle. While still following reactive programming principles, R2DBC provides the ability to maintain a minimum pool of connections for consistent availability and performance.