Spring HikariDataSource Hikari数据库连接池监控 总连接数/活跃连接数/空闲连接数/阻塞在获取连接的线程数

博文目录

文章目录


@Autowired
    private HikariDataSource dataSource;

    @PostConstruct
    public void monitor() {
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(() -> {
            HikariPoolMXBean hikariPoolMXBean = dataSource.getHikariPoolMXBean();
            if (null != hikariPoolMXBean) {
                log.info("total:{}, active:{}, idle:{}, awaiting:{}", 
                		hikariPoolMXBean.getTotalConnections(),
                        hikariPoolMXBean.getActiveConnections(),
                        hikariPoolMXBean.getIdleConnections(),
                        hikariPoolMXBean.getThreadsAwaitingConnection());
            }
        }, 0, 100, TimeUnit.MILLISECONDS);
    }

    @Autowired
    private BidUserMapper mapper;

    @PostConstruct
    public void test() {
        ExecutorService executor = Executors.newFixedThreadPool(100);
        for (int i = 0; i < 49; i++) {
            executor.submit((Runnable) () -> {
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (Throwable cause) {
                    log.error("", cause);
                }
                log.info("111");
                while (true) {
                    try {
                        mapper.selectById(9527);
                    } catch (Throwable cause) {
                        log.error("", cause);
                    }
                }
            });
        }
    }

注意

  • 监控值(空闲连接数 ,活动连接,总连接数,等待连接的线程数)是非常短暂的。由于每个统计数据都是时间敏感的,并且是独立收集的,所以在短时间内可能无法“累计”。不要基于这些值进行任何编程决策,它们仅用于监视。
  • 配置 spring.datasource.hikari.register-mbeans=true
  • 必须得至少获取一个连接, 不然会一直报错, 在这里我启动了另一个定时任务执行查询操作
    java.lang.reflect.UndeclaredThrowableException: null
    	at com.sun.proxy.$Proxy174.getTotalConnections(Unknown Source) ~[?:?]
    	at com.bestpay.redbag.web.controller.MrathenaController.datasource(MrathenaController.java:38) ~[classes/:?]
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202]
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_202]
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_202]
    	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_202]
    	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_202]
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_202]
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) ~[?:1.8.0_202]
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[?:1.8.0_202]
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[?:1.8.0_202]
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[?:1.8.0_202]
    	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_202]
    Caused by: javax.management.InstanceNotFoundException: com.zaxxer.hikari:type=Pool (hikari)
    	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095) ~[?:1.8.0_202]
    	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:643) ~[?:1.8.0_202]
    	at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678) ~[?:1.8.0_202]
    	at com.sun.jmx.mbeanserver.MXBeanProxy$GetHandler.invoke(MXBeanProxy.java:122) ~[?:1.8.0_202]
    	at com.sun.jmx.mbeanserver.MXBeanProxy.invoke(MXBeanProxy.java:167) ~[?:1.8.0_202]
    	at javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:258) ~[?:1.8.0_202]
    	... 16 more
    

可配调整 spring.datasource.hikari.maximum-pool-size=50, spring.datasource.hikari.minimum-idle=50, spring.datasource.hikari.connection-timeout=250 等配置来测试效果

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值