【问题标题】:get active connection on HikariDataSource在 HikariDataSource 上获得活动连接
【发布时间】:2014-06-12 15:09:18
【问题描述】:

我试图弄清楚当前打开了多少个连接,但我似乎找不到使用 Hikari 的明显方法。

HikariPool 公开了该信息 (getActiveConnections),但我没有看到从 HikariDataSource 访问该信息的简单方法。

【问题讨论】:

    标签: hikaricp


    【解决方案1】:

    如果你使用的是弹簧靴:

    new HikariDataSourcePoolMetadata(dataSource).getActive();
    

    【讨论】:

    • 啊,啊。 I know, thanks!
    • 在我的情况下,我必须添加转换 - new HikariDataSourcePoolMetadata((HikariDataSource) dataSource).getMax()
    【解决方案2】:

    您必须通过 JMX 编程访问来获取它。首先,通过registerMbeans 属性或调用setRegisterMeans() 启用MBean 注册。然后参考此页面了解如何执行编程访问:

    https://github.com/brettwooldridge/HikariCP/wiki/JMX-Monitoring

    【讨论】:

    • 感谢布雷特的回答。我正在自己创建和管理HikariDataSource。如果这很“简单”,您不能使用 API 访问这些信息吗?我不能也不会为此使用 JMX。谢谢。
    • 我会考虑在即将发布的版本中使用它。添加 API 的事情是,当您有一个成千上万的用户正在使用的项目时,您最好对您选择的内容 100% 满意,因为一旦它在野外进行更改就非常困难。在您的场景中需要活动连接的用例是什么?
    • 检查#1013Spring Boot project问题
    【解决方案3】:

    您可以使用以下类进行更好的监控:

                import javax.sql.DataSource;
                import org.aspectj.lang.JoinPoint;
                import org.aspectj.lang.annotation.After;
                import org.aspectj.lang.annotation.Aspect;
                import org.aspectj.lang.annotation.Before;
                import org.springframework.beans.factory.annotation.Autowired;
                import org.springframework.stereotype.Component;
                import com.zaxxer.hikari.HikariDataSource;
                import com.zaxxer.hikari.pool.HikariPool;
                import lombok.extern.slf4j.Slf4j;
    
    
                @Aspect
                @Component
                @Slf4j
                public class DataSourceAspectLogger {
    
                    private HikariPool pool;
    
                    @Autowired
                    private HikariDataSource ds;
    
                    @Before("execution(* com.x.common.sql.repo.*.*(..))")
                    public void logBeforeConnection(JoinPoint jp) throws Throwable {
                        logDataSourceInfos("Before", jp);
                    }
    
                    @After("execution(* com.x.common.sql.repo.*.*(..)) ")
                    public void logAfterConnection(JoinPoint jp) throws Throwable {
                        logDataSourceInfos("After", jp);
                    }
    
                    @Autowired
                    public void getPool() {
                        try {
                            java.lang.reflect.Field field = ds.getClass().getDeclaredField("pool");
                            field.setAccessible(true);
                            this.pool = (HikariPool) field.get(ds);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
    
                    public void logDataSourceInfos(final String time, final JoinPoint jp) {
                        final String method = String.format("%s:%s", jp.getTarget().getClass().getName(), jp.getSignature().getName());
                        int totalConnections = pool.getTotalConnections();
                        int activeConnections = pool.getActiveConnections();
                        int freeConnections = totalConnections - activeConnections;
                        int connectionWaiting = pool.getThreadsAwaitingConnection();
                        log.info(String.format("%s %s: number of connections in use by the application (active): %d.", time, method, activeConnections));
                        log.info(String.format("%s %s: the number of established but idle connections: %d.", time, method, freeConnections));
                        log.info(String.format("%s %s: number of threads waiting for a connection: %d.", time, method, connectionWaiting));
                        log.info(String.format("%s %s: max pool size: %d.", time, method, ds.getMaximumPoolSize()));
                    }
                }
    

    【讨论】:

      【解决方案4】:

      这可以非常直接地完成。

      dataSource.hikariPoolMXBean.activeConnections
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-16
        • 2011-11-25
        • 2020-09-01
        • 1970-01-01
        • 2017-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多