Ultimate JDBC Connection Pool - Java 6 compatible version providing high-performance database connection pooling
—
Runtime monitoring and configuration through JMX MBeans, providing comprehensive visibility into pool statistics, connection health, and the ability to modify configuration parameters during runtime without application restarts.
JMX MBean interface for runtime configuration management, allowing dynamic modification of key pool parameters during application execution.
public interface HikariConfigMXBean {
/**
* Get connection acquisition timeout in milliseconds.
*
* @return timeout in milliseconds
*/
long getConnectionTimeout();
/**
* Set connection acquisition timeout in milliseconds.
*
* @param connectionTimeoutMs timeout in milliseconds
*/
void setConnectionTimeout(long connectionTimeoutMs);
/**
* Get connection validation timeout in milliseconds.
*
* @return timeout in milliseconds
*/
long getValidationTimeout();
/**
* Set connection validation timeout in milliseconds.
*
* @param validationTimeoutMs timeout in milliseconds
*/
void setValidationTimeout(long validationTimeoutMs);
/**
* Get idle connection timeout in milliseconds.
*
* @return timeout in milliseconds
*/
long getIdleTimeout();
/**
* Set idle connection timeout in milliseconds.
*
* @param idleTimeoutMs timeout in milliseconds
*/
void setIdleTimeout(long idleTimeoutMs);
/**
* Get connection leak detection threshold in milliseconds.
*
* @return threshold in milliseconds, 0 = disabled
*/
long getLeakDetectionThreshold();
/**
* Set connection leak detection threshold in milliseconds.
*
* @param leakDetectionThresholdMs threshold in milliseconds, 0 to disable
*/
void setLeakDetectionThreshold(long leakDetectionThresholdMs);
/**
* Get maximum connection lifetime in milliseconds.
*
* @return lifetime in milliseconds
*/
long getMaxLifetime();
/**
* Set maximum connection lifetime in milliseconds.
*
* @param maxLifetimeMs lifetime in milliseconds
*/
void setMaxLifetime(long maxLifetimeMs);
/**
* Get minimum number of idle connections.
*
* @return minimum idle connections
*/
int getMinimumIdle();
/**
* Set minimum number of idle connections.
*
* @param minIdle minimum idle connections
*/
void setMinimumIdle(int minIdle);
/**
* Get maximum pool size.
*
* @return maximum pool size
*/
int getMaximumPoolSize();
/**
* Set maximum pool size.
*
* @param maxPoolSize maximum pool size
*/
void setMaximumPoolSize(int maxPoolSize);
/**
* Get pool name (read-only).
*
* @return pool name
*/
String getPoolName();
}JMX MBean interface for pool monitoring and management, providing real-time statistics and operational control over the connection pool.
public interface HikariPoolMXBean {
/**
* Get number of idle connections in the pool.
*
* @return count of idle connections
*/
int getIdleConnections();
/**
* Get number of active (in-use) connections.
*
* @return count of active connections
*/
int getActiveConnections();
/**
* Get total number of connections in the pool.
*
* @return total connection count (active + idle)
*/
int getTotalConnections();
/**
* Get number of threads waiting for connections.
*
* @return count of threads awaiting connections
*/
int getThreadsAwaitingConnection();
/**
* Soft evict idle connections from the pool.
* Removes idle connections that have exceeded their idle timeout.
*/
void softEvictConnections();
/**
* Suspend the connection pool.
* Prevents new connections from being allocated while allowing
* existing connections to continue operating.
*/
void suspendPool();
/**
* Resume the connection pool after suspension.
* Re-enables connection allocation.
*/
void resumePool();
}import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
// Enable MBean registration in configuration
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost/mydb");
config.setUsername("user");
config.setPassword("password");
config.setPoolName("MyAppPool"); // Important: set pool name for JMX identification
config.setRegisterMbeans(true); // Enable JMX MBean registration
HikariDataSource dataSource = new HikariDataSource(config);import javax.management.MBeanServer;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import com.zaxxer.hikari.HikariConfigMXBean;
import com.zaxxer.hikari.pool.HikariPoolMXBean;
// Get the platform MBean server
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
// Access configuration MBean
ObjectName configObjectName = new ObjectName("com.zaxxer.hikari:type=Pool (MyAppPool)");
HikariConfigMXBean configMBean = MBeanServerInvocationHandler.newProxyInstance(
server, configObjectName, HikariConfigMXBean.class, false);
// Access pool monitoring MBean
ObjectName poolObjectName = new ObjectName("com.zaxxer.hikari:type=PoolStats (MyAppPool)");
HikariPoolMXBean poolMBean = MBeanServerInvocationHandler.newProxyInstance(
server, poolObjectName, HikariPoolMXBean.class, false);
// Read current configuration
long connectionTimeout = configMBean.getConnectionTimeout();
int maxPoolSize = configMBean.getMaximumPoolSize();
// Monitor pool statistics
int activeConnections = poolMBean.getActiveConnections();
int idleConnections = poolMBean.getIdleConnections();
int totalConnections = poolMBean.getTotalConnections();
int waitingThreads = poolMBean.getThreadsAwaitingConnection();
System.out.println("Pool Stats:");
System.out.println(" Active: " + activeConnections);
System.out.println(" Idle: " + idleConnections);
System.out.println(" Total: " + totalConnections);
System.out.println(" Waiting Threads: " + waitingThreads);// Dynamically adjust pool configuration
configMBean.setMaximumPoolSize(25); // Increase max pool size
configMBean.setConnectionTimeout(45000); // Increase connection timeout to 45 seconds
configMBean.setIdleTimeout(900000); // Increase idle timeout to 15 minutes
// Enable leak detection if not already enabled
configMBean.setLeakDetectionThreshold(120000); // 2 minutes// Suspend pool during maintenance
poolMBean.suspendPool();
System.out.println("Pool suspended - no new connections will be allocated");
// Perform maintenance tasks...
// Resume pool operations
poolMBean.resumePool();
System.out.println("Pool resumed - normal operations restored");
// Force eviction of idle connections
poolMBean.softEvictConnections();
System.out.println("Idle connections evicted");import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
// Create monitoring service
ScheduledExecutorService monitoring = Executors.newScheduledThreadPool(1);
// Monitor pool health every 30 seconds
monitoring.scheduleAtFixedRate(() -> {
try {
int active = poolMBean.getActiveConnections();
int total = poolMBean.getTotalConnections();
int waiting = poolMBean.getThreadsAwaitingConnection();
// Calculate pool utilization
double utilization = total > 0 ? (double) active / total : 0.0;
// Alert on high utilization
if (utilization > 0.8) {
System.err.println("WARNING: High pool utilization: " +
String.format("%.1f%% (%d/%d)", utilization * 100, active, total));
}
// Alert on waiting threads
if (waiting > 0) {
System.err.println("WARNING: " + waiting + " threads waiting for connections");
}
// Log current status
System.out.println("Pool Status - Active: " + active +
", Total: " + total +
", Utilization: " + String.format("%.1f%%", utilization * 100));
} catch (Exception e) {
System.err.println("Error monitoring pool: " + e.getMessage());
}
}, 0, 30, TimeUnit.SECONDS);When JMX MBeans are enabled, HikariCP pools appear in JConsole under the com.zaxxer.hikari domain:
Configuration MBean: com.zaxxer.hikari:type=Pool (PoolName)
Pool Statistics MBean: com.zaxxer.hikari:type=PoolStats (PoolName)
// In Spring Boot, enable JMX management in application.properties:
// management.endpoints.jmx.exposure.include=*
// spring.jmx.enabled=true
@Component
public class HikariMonitor {
@Autowired
private HikariDataSource dataSource;
@Autowired
private MBeanServer mBeanServer;
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
// Access HikariCP MBeans after application startup
String poolName = dataSource.getPoolName();
ObjectName configMBean = new ObjectName(
"com.zaxxer.hikari:type=Pool (" + poolName + ")");
ObjectName poolMBean = new ObjectName(
"com.zaxxer.hikari:type=PoolStats (" + poolName + ")");
// Register custom monitoring logic
startPoolMonitoring(configMBean, poolMBean);
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-zaxxer--hikari-cp-java6