Implementation:Apache Dolphinscheduler DataSourceClientProvider Cache Lifecycle
| Knowledge Sources | |
|---|---|
| Domains | Connection_Management, Resource_Management |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for managing connection pool lifecycle using DataSourceClientProvider's Guava Cache with RemovalListener and BasePooledDataSourceClient.close() for HikariCP pool shutdown.
Description
'DataSourceClientProvider at L47-64 configures a Guava Cache with expireAfterAccess using DataSourceConstants.DATASOURCE_CLIENT_EXPIRE_SECONDS. The RemovalListener<String, PooledDataSourceClient> calls client.close() on eviction. BasePooledDataSourceClient.close() at L80-87 delegates to HikariDataSource.close() which terminates all idle and in-use connections. Configuration constants from DataSourceConstants control pool sizing: DATASOURCE_POOL_MAX_ACTIVE (maximum pool size), DATASOURCE_POOL_MIN_IDLE (minimum idle connections).
Usage
Automatic. No manual cleanup required. The cache eviction handles pool lifecycle transparently.
Code Reference
Source Location
- Repository: dolphinscheduler
- File: dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/main/java/org/apache/dolphinscheduler/plugin/datasource/api/plugin/DataSourceClientProvider.java (L47-64)
- File: dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/main/java/org/apache/dolphinscheduler/plugin/datasource/api/client/BasePooledDataSourceClient.java (L80-87)
Signature
@Component
public class DataSourceClientProvider {
// Guava cache with TTL-based eviction and cleanup listener
private static final Cache<String, PooledDataSourceClient>
POOLED_DATASOURCE_CLIENT_CACHE = CacheBuilder.newBuilder()
.expireAfterAccess(
DataSourceConstants.DATASOURCE_CLIENT_EXPIRE_SECONDS,
TimeUnit.SECONDS)
.removalListener((RemovalListener<String, PooledDataSourceClient>)
notification -> {
if (notification.getValue() != null) {
notification.getValue().close();
}
})
.build();
}
public class BasePooledDataSourceClient {
@Override
public void close() {
if (hikariDataSource != null) {
hikariDataSource.close(); // releases all connections
}
}
}
Import
import org.apache.dolphinscheduler.plugin.datasource.api.plugin.DataSourceClientProvider;
import org.apache.dolphinscheduler.plugin.datasource.api.constants.DataSourceConstants;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Cache access patterns | get/put | Automatic | TTL refreshed on each access |
| DATASOURCE_CLIENT_EXPIRE_SECONDS | long | Configuration | Cache TTL in seconds |
| DATASOURCE_POOL_MAX_ACTIVE | int | Configuration | Maximum HikariCP pool size |
| DATASOURCE_POOL_MIN_IDLE | int | Configuration | Minimum idle connections in pool |
Outputs
| Name | Type | Description |
|---|---|---|
| Pool cleanup | Side effect | HikariDataSource.close() called on eviction |
| Connection release | Side effect | All pooled JDBC connections released |
Usage Examples
Automatic Lifecycle
// Access 1: Pool created for MySQL datasource "ds_1"
Connection conn1 = provider.getPooledConnection(DbType.MYSQL, param);
// HikariCP pool created, cached in Guava cache
// Access 2 (within TTL): Pool reused
Connection conn2 = provider.getPooledConnection(DbType.MYSQL, param);
// Same HikariCP pool, TTL refreshed
// No access for DATASOURCE_CLIENT_EXPIRE_SECONDS...
// Guava triggers eviction:
// RemovalListener -> client.close() -> hikariDataSource.close()
// All connections released back to database server