CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-zaxxer--hikari-cp

High-performance JDBC connection pool library for Java applications

Pending
Overview
Eval results
Files

jmx-management.mddocs/

JMX Management

Runtime monitoring and management through JMX MBeans for operational visibility and control.

Capabilities

HikariPoolMXBean

Pool statistics and management operations through JMX.

/**
 * JMX management interface for HikariCP pool statistics and operations
 * Provides real-time pool metrics and control operations
 */
public interface HikariPoolMXBean {
    
    /**
     * Get the number of currently idle connections in the pool
     * Note: Values are transient point-in-time measurements
     * @return current idle connection count
     */
    int getIdleConnections();
    
    /**
     * Get the number of currently active (in-use) connections
     * Note: Values are transient point-in-time measurements  
     * @return current active connection count
     */
    int getActiveConnections();
    
    /**
     * Get total number of connections in the pool (idle + active)
     * Note: Due to timing, sum of idle + active may not equal total
     * @return total connection count
     */
    int getTotalConnections();
    
    /**
     * Get number of threads waiting for connections from the pool
     * @return thread count awaiting connections
     */
    int getThreadsAwaitingConnection();
    
    /**
     * Evict currently idle connections and mark active connections for eviction
     * Active connections will be evicted when returned to pool
     */
    void softEvictConnections();
    
    /**
     * Suspend the pool - new getConnection() calls will block indefinitely
     * Requires setAllowPoolSuspension(true) in configuration
     */
    void suspendPool();
    
    /**
     * Resume pool operations after suspension
     * Requires setAllowPoolSuspension(true) in configuration
     */
    void resumePool();
}

JMX Management Examples:

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

// Access pool MBean through HikariDataSource
HikariDataSource dataSource = new HikariDataSource(config);
HikariPoolMXBean poolBean = dataSource.getHikariPoolMXBean();

// Monitor pool statistics
System.out.printf("Pool Status:%n");
System.out.printf("  Active: %d%n", poolBean.getActiveConnections());
System.out.printf("  Idle: %d%n", poolBean.getIdleConnections());
System.out.printf("  Total: %d%n", poolBean.getTotalConnections());
System.out.printf("  Waiting: %d%n", poolBean.getThreadsAwaitingConnection());

// Maintenance operations
poolBean.softEvictConnections(); // Force connection refresh

// Pool suspension for maintenance
if (config.isAllowPoolSuspension()) {
    poolBean.suspendPool();
    // Perform maintenance
    poolBean.resumePool();
}

// Access via JMX directly (when registerMbeans=true)
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (MyPool)");
HikariPoolMXBean proxy = MBeanServerInvocationHandler.newProxyInstance(
    server, poolName, HikariPoolMXBean.class, false);

HikariConfigMXBean

Runtime configuration management through JMX.

/**
 * JMX management interface for HikariCP runtime configuration
 * Allows modification of certain properties at runtime
 */
public interface HikariConfigMXBean {
    
    /**
     * Connection timeout configuration
     */
    long getConnectionTimeout();
    void setConnectionTimeout(long connectionTimeoutMs);
    
    /**
     * Validation timeout configuration
     */
    long getValidationTimeout();
    void setValidationTimeout(long validationTimeoutMs);
    
    /**
     * Idle timeout configuration
     */
    long getIdleTimeout();
    void setIdleTimeout(long idleTimeoutMs);
    
    /**
     * Leak detection threshold configuration
     */
    long getLeakDetectionThreshold();
    void setLeakDetectionThreshold(long leakDetectionThresholdMs);
    
    /**
     * Maximum connection lifetime configuration
     */
    long getMaxLifetime();
    void setMaxLifetime(long maxLifetimeMs);
    
    /**
     * Pool sizing configuration
     */
    int getMinimumIdle();
    void setMinimumIdle(int minIdle);
    
    int getMaximumPoolSize();
    void setMaximumPoolSize(int maxPoolSize);
    
    /**
     * Runtime credential updates (DataSource-based connections only)
     */
    void setPassword(String password);
    void setUsername(String username);
    void setCredentials(Credentials credentials);
    
    /**
     * Pool identification
     */
    String getPoolName();
    
    /**
     * Catalog configuration (runtime changes require pool suspension)
     */
    String getCatalog();
    void setCatalog(String catalog);
}

Runtime Configuration Examples:

// Access config MBean through HikariDataSource
HikariConfigMXBean configBean = dataSource.getHikariConfigMXBean();

// Adjust pool size based on load
configBean.setMaximumPoolSize(30); // Increase max pool size
configBean.setMinimumIdle(10);     // Increase minimum idle connections

// Adjust timeouts for different environments
configBean.setConnectionTimeout(60000);        // 1 minute for slow networks
configBean.setLeakDetectionThreshold(300000);  // 5 minutes for long operations

// Update credentials at runtime (DataSource-based only)
configBean.setPassword("newPassword");

// Monitor current settings
System.out.printf("Current pool size: %d-%d%n", 
    configBean.getMinimumIdle(), 
    configBean.getMaximumPoolSize());

JMX Registration

Enable automatic MBean registration for remote monitoring.

/**
 * Enable MBean registration in HikariConfig
 */
HikariConfig config = new HikariConfig();
config.setRegisterMbeans(true);  // Enable JMX registration
config.setPoolName("MyApp-DB");  // Required for JMX (no ':' allowed)

HikariDataSource dataSource = new HikariDataSource(config);
// MBeans automatically registered with names:
// - com.zaxxer.hikari:type=Pool (MyApp-DB)
// - com.zaxxer.hikari:type=PoolConfig (MyApp-DB)

JMX Console Access:

import javax.management.*;
import java.lang.management.ManagementFactory;

public class HikariJMXMonitor {
    
    public void monitorPool(String poolName) throws Exception {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        
        // Pool statistics MBean
        ObjectName poolObjectName = new ObjectName(
            "com.zaxxer.hikari:type=Pool (" + poolName + ")");
        
        // Configuration MBean  
        ObjectName configObjectName = new ObjectName(
            "com.zaxxer.hikari:type=PoolConfig (" + poolName + ")");
        
        // Read pool statistics
        Integer active = (Integer) server.getAttribute(poolObjectName, "ActiveConnections");
        Integer idle = (Integer) server.getAttribute(poolObjectName, "IdleConnections");
        Integer total = (Integer) server.getAttribute(poolObjectName, "TotalConnections");
        
        System.out.printf("Pool Stats - Active: %d, Idle: %d, Total: %d%n", active, idle, total);
        
        // Invoke management operations
        server.invoke(poolObjectName, "softEvictConnections", null, null);
        
        // Update configuration
        server.setAttribute(configObjectName, 
            new Attribute("MaximumPoolSize", 25));
    }
}

Health Monitoring

Implement health checks using JMX metrics.

import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.HikariPoolMXBean;

public class PoolHealthMonitor {
    
    private final HikariDataSource dataSource;
    private final HikariPoolMXBean poolBean;
    
    public PoolHealthMonitor(HikariDataSource dataSource) {
        this.dataSource = dataSource;
        this.poolBean = dataSource.getHikariPoolMXBean();
    }
    
    /**
     * Check overall pool health
     */
    public PoolHealth checkHealth() {
        if (!dataSource.isRunning()) {
            return PoolHealth.DOWN;
        }
        
        int waiting = poolBean.getThreadsAwaitingConnection();
        int active = poolBean.getActiveConnections();
        int total = poolBean.getTotalConnections();
        
        // Check for connection starvation
        if (waiting > 0) {
            return PoolHealth.DEGRADED;
        }
        
        // Check for pool exhaustion (>90% utilization)
        if (total > 0 && (double) active / total > 0.9) {
            return PoolHealth.WARNING;
        }
        
        return PoolHealth.HEALTHY;
    }
    
    /**
     * Get detailed health metrics
     */
    public HealthMetrics getMetrics() {
        return new HealthMetrics(
            poolBean.getActiveConnections(),
            poolBean.getIdleConnections(),
            poolBean.getTotalConnections(), 
            poolBean.getThreadsAwaitingConnection(),
            dataSource.isRunning()
        );
    }
    
    public enum PoolHealth {
        HEALTHY, WARNING, DEGRADED, DOWN
    }
    
    public static class HealthMetrics {
        public final int activeConnections;
        public final int idleConnections;
        public final int totalConnections;
        public final int threadsWaiting;
        public final boolean isRunning;
        
        public HealthMetrics(int active, int idle, int total, int waiting, boolean running) {
            this.activeConnections = active;
            this.idleConnections = idle;
            this.totalConnections = total;
            this.threadsWaiting = waiting;
            this.isRunning = running;
        }
    }
}

Automated Pool Management

Use JMX for automated pool management based on metrics.

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class AutoPoolManager {
    
    private final HikariDataSource dataSource;
    private final HikariPoolMXBean poolBean;
    private final HikariConfigMXBean configBean;
    private final ScheduledExecutorService scheduler;
    
    public AutoPoolManager(HikariDataSource dataSource) {
        this.dataSource = dataSource;
        this.poolBean = dataSource.getHikariPoolMXBean();
        this.configBean = dataSource.getHikariConfigMXBean();
        this.scheduler = Executors.newSingleThreadScheduledExecutor();
    }
    
    /**
     * Start automated pool management
     */
    public void startAutoManagement() {
        scheduler.scheduleAtFixedRate(this::managePool, 30, 30, TimeUnit.SECONDS);
    }
    
    private void managePool() {
        try {
            int active = poolBean.getActiveConnections();
            int total = poolBean.getTotalConnections();
            int waiting = poolBean.getThreadsAwaitingConnection();
            int maxSize = configBean.getMaximumPoolSize();
            
            // Scale up if connections are being waited for
            if (waiting > 0 && maxSize < 50) {
                int newMax = Math.min(maxSize + 5, 50);
                configBean.setMaximumPoolSize(newMax);
                System.out.printf("Scaled up pool to %d connections%n", newMax);
            }
            
            // Scale down if utilization is consistently low
            else if (total > 10 && active < total * 0.3 && maxSize > 10) {
                int newMax = Math.max(maxSize - 2, 10);
                configBean.setMaximumPoolSize(newMax);
                System.out.printf("Scaled down pool to %d connections%n", newMax);
            }
            
            // Refresh connections if pool is idle
            if (active == 0 && total > 0) {
                poolBean.softEvictConnections();
                System.out.println("Refreshed idle connections");
            }
            
        } catch (Exception e) {
            System.err.println("Error in pool management: " + e.getMessage());
        }
    }
    
    public void shutdown() {
        scheduler.shutdown();
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-com-zaxxer--hikari-cp

docs

configuration.md

connection-pooling.md

hibernate-integration.md

index.md

jmx-management.md

metrics-integration.md

utilities.md

tile.json