High-performance JDBC connection pool library for Java applications
—
Core connection pool functionality for creating and managing database connections efficiently with minimal overhead.
The main entry point for HikariCP providing DataSource implementation with connection pooling.
/**
* The HikariCP pooled DataSource implementation
* Extends HikariConfig to inherit all configuration capabilities
* Implements standard JDBC DataSource interface
*/
public class HikariDataSource extends HikariConfig implements DataSource, Closeable {
/**
* Default constructor for lazy initialization
* Pool starts on first getConnection() call
*/
public HikariDataSource();
/**
* Pre-configured constructor that starts the pool immediately
* @param configuration HikariConfig instance with connection settings
*/
public HikariDataSource(HikariConfig configuration);
/**
* Get a connection from the pool
* @return Connection from the pool
* @throws SQLException if connection cannot be obtained
*/
public Connection getConnection() throws SQLException;
/**
* DataSource method for username/password connections
* @throws SQLFeatureNotSupportedException - not supported by HikariCP
*/
public Connection getConnection(String username, String password) throws SQLException;
/**
* Check if the pool is running and not suspended or shutdown
* @return true if pool is operational
*/
public boolean isRunning();
/**
* Evict a specific connection from the pool
* @param connection the connection to evict
*/
public void evictConnection(Connection connection);
/**
* Shutdown the DataSource and its connection pool
*/
public void close();
/**
* Check if the DataSource has been closed
* @return true if closed
*/
public boolean isClosed();
/**
* Get the pool management MXBean
* @return HikariPoolMXBean for JMX management
*/
public HikariPoolMXBean getHikariPoolMXBean();
/**
* Get the configuration management MXBean
* @return HikariConfigMXBean for JMX configuration management
*/
public HikariConfigMXBean getHikariConfigMXBean();
}Usage Examples:
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
// Lazy initialization approach
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:postgresql://localhost/test");
dataSource.setUsername("postgres");
dataSource.setPassword("password");
dataSource.setMaximumPoolSize(10);
// Pool starts when first connection is requested
Connection conn = dataSource.getConnection();
// Pre-configured approach
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
config.setUsername("user");
config.setPassword("pass");
config.setMaximumPoolSize(20);
config.setConnectionTimeout(30000);
// Pool starts immediately
HikariDataSource dataSource = new HikariDataSource(config);
// Check pool status
if (dataSource.isRunning()) {
System.out.println("Pool is operational");
}
// Clean shutdown
dataSource.close();HikariCP provides sophisticated connection management with leak detection and proxy wrapping.
Connection Lifecycle:
// Obtain connection from pool
try (Connection connection = dataSource.getConnection()) {
// Connection is wrapped in HikariCP proxy
// Automatically returned to pool when closed
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM users WHERE id = ?");
stmt.setLong(1, userId);
ResultSet rs = stmt.executeQuery();
// Process results
while (rs.next()) {
// Handle row data
}
// Statements and ResultSets are also proxied for proper cleanup
} // Connection automatically returned to pool hereConnection Eviction:
// Force eviction of specific connection
Connection problematicConnection = dataSource.getConnection();
// ... detect connection issue
dataSource.evictConnection(problematicConnection);
// Soft eviction of all idle connections (via JMX)
HikariPoolMXBean poolMBean = dataSource.getHikariPoolMXBean();
poolMBean.softEvictConnections(); // Evicts idle connections immediatelyHikariDataSource implements the standard JDBC DataSource interface.
// Standard DataSource methods
public PrintWriter getLogWriter() throws SQLException;
public void setLogWriter(PrintWriter out) throws SQLException;
public void setLoginTimeout(int seconds) throws SQLException;
public int getLoginTimeout() throws SQLException;
// JDBC 4.1 methods
public Logger getParentLogger() throws SQLFeatureNotSupportedException; // Not supported
// Wrapper methods for accessing underlying DataSource
public <T> T unwrap(Class<T> iface) throws SQLException;
public boolean isWrapperFor(Class<?> iface) throws SQLException;Integration with Application Servers:
// For application server integration
import javax.naming.InitialContext;
import javax.sql.DataSource;
// DataSource can be bound to JNDI
InitialContext ctx = new InitialContext();
ctx.bind("java:comp/env/jdbc/MyDB", dataSource);
// Later retrieval
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");Real-time pool statistics available through the management interface.
/**
* Get real-time pool statistics
*/
HikariPoolMXBean poolMBean = dataSource.getHikariPoolMXBean();
int active = poolMBean.getActiveConnections(); // Connections in use
int idle = poolMBean.getIdleConnections(); // Available connections
int total = poolMBean.getTotalConnections(); // Total pool size
int waiting = poolMBean.getThreadsAwaitingConnection(); // Threads waiting for connectionsMonitoring Example:
import com.zaxxer.hikari.HikariPoolMXBean;
public void monitorPool(HikariDataSource dataSource) {
HikariPoolMXBean poolBean = dataSource.getHikariPoolMXBean();
if (poolBean != null) {
System.out.printf("Pool Stats - Active: %d, Idle: %d, Total: %d, Waiting: %d%n",
poolBean.getActiveConnections(),
poolBean.getIdleConnections(),
poolBean.getTotalConnections(),
poolBean.getThreadsAwaitingConnection());
// Check for potential issues
if (poolBean.getThreadsAwaitingConnection() > 0) {
System.out.println("Warning: Threads waiting for connections");
}
}
}HikariCP supports pool suspension for maintenance operations.
/**
* Suspend and resume pool operations
* Requires setAllowPoolSuspension(true) in configuration
*/
HikariPoolMXBean poolMBean = dataSource.getHikariPoolMXBean();
// Suspend pool - new getConnection() calls will block
poolMBean.suspendPool();
// Perform maintenance operations
// ...
// Resume normal operations
poolMBean.resumePool();Maintenance Example:
public void performMaintenance(HikariDataSource dataSource) {
HikariPoolMXBean poolBean = dataSource.getHikariPoolMXBean();
try {
// Suspend pool to prevent new connections
poolBean.suspendPool();
System.out.println("Pool suspended for maintenance");
// Evict all idle connections
poolBean.softEvictConnections();
// Perform database maintenance
// ...
} finally {
// Always resume the pool
poolBean.resumePool();
System.out.println("Pool resumed");
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-zaxxer--hikari-cp