You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用如何日志打印Websphere连接池活动连接等信息

WebSphere Liberty环境下打印连接池活动连接等指标到应用日志的实现方案

适配环境:WebSphere Liberty 20.0.0.12、Spring Boot 2.0.5,方案不适用于传统WebSphere Application Server

方案一:自定义定时任务采集MBean指标(推荐,生产可用)

你既然已经能在JConsole中看到连接池信息,说明Liberty已经默认暴露了连接池的JMX MBean,直接通过代码读取MBean属性定时打印即可,不需要额外引入第三方依赖。

  1. 先确认Liberty server.xml配置,开启连接池统计开关
    确保featureManager中加载了监控特性,同时给对应数据源的连接管理器开启统计能力:
    <featureManager>
        <!-- 原有业务特性保留,追加监控特性 -->
        <feature>monitor-1.0</feature>
    </featureManager>
    
    <!-- 你的业务数据源配置,在connectionManager节点追加enableStatisticsForPool="true" -->
    <dataSource id="appDataSource" jndiName="jdbc/appDB">
        <!-- 数据库驱动、连接URL、账号密码等原有配置保持不变 -->
        <connectionManager maxPoolSize="50" minPoolSize="5" agedTimeout="30m"
                            enableStatisticsForPool="true"/>
    </dataSource>
    
  2. 在Spring Boot项目中添加定时采集逻辑,直接把指标打到业务应用日志中
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import javax.management.MBeanServer;
    import javax.management.ObjectName;
    import java.lang.management.ManagementFactory;
    import java.util.Set;
    
    @Component
    @EnableScheduling
    public class PoolMetricsLogger {
        private static final Logger log = LoggerFactory.getLogger(PoolMetricsLogger.class);
        private static final ObjectName POOL_STATS_MBEAN;
    
        static {
            try {
                POOL_STATS_MBEAN = new ObjectName("WebSphere:type=JDBCConnectionPoolStats,name=*");
            } catch (Exception e) {
                throw new RuntimeException("初始化连接池MBean对象失败", e);
            }
        }
    
        // 采集频率可自行调整,示例为每30秒打印一次
        @Scheduled(fixedRate = 30000)
        public void printPoolStats() {
            try {
                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
                Set<ObjectName> poolBeans = mbs.queryNames(POOL_STATS_MBEAN, null);
                for (ObjectName pool : poolBeans) {
                    int activeConn = (int) mbs.getAttribute(pool, "ActiveCount");
                    int freeConn = (int) mbs.getAttribute(pool, "FreeCount");
                    int waitQueueLen = (int) mbs.getAttribute(pool, "WaitQueueLength");
                    long totalCreate = (long) mbs.getAttribute(pool, "CreateCount");
                    long totalDestroy = (long) mbs.getAttribute(pool, "DestroyCount");
                    long avgWaitMs = (long) mbs.getAttribute(pool, "WaitTimeAverage");
    
                    log.info("Liberty连接池监控 | 池名:{} | 活动连接数:{} | 空闲连接数:{} | 等待队列长度:{} | 累计创建连接:{} | 累计销毁连接:{} | 平均获取连接等待时长(ms):{}",
                            pool.getKeyProperty("name"), activeConn, freeConn, waitQueueLen,
                            totalCreate, totalDestroy, avgWaitMs);
                }
            } catch (Exception e) {
                log.warn("采集连接池指标异常", e);
            }
        }
    }
    
    你在JConsole中能看到的所有连接池属性,都可以直接在这个MBean中读取追加到日志里,比如连接泄漏计数、连接使用时长统计等都支持。

方案二:开启Liberty原生JDBC跟踪日志(仅排查问题临时使用)

如果不想写代码,可以直接修改Liberty的日志跟踪级别,让服务器直接打印连接池全链路日志到Liberty自带的messages.log中:

<logging traceSpecification="*=info:com.ibm.ws.jdbc.*=debug:com.ibm.ws.rsadapter.pool.*=debug"/>

注意:该配置会输出连接申请、释放、创建、销毁、超时的全量调试日志,日志量非常大,生产环境禁止长期开启,仅在排查连接泄漏、连接池耗尽等问题时临时打开,排查完成后立即回退配置。

注意事项

  • 不要套用传统WebSphere Application Server的MBean路径采集指标,Liberty的MBean命名规则、属性名和传统WAS完全不同,传统WAS的WebSphere:type=DataSource,*路径在Liberty下无法获取到连接池统计数据
  • 如果是Spring Boot以可执行jar包形式内嵌Liberty运行,只需要把上述server.xml的配置对应到内嵌容器的配置项即可,采集代码不需要改动

内容的提问来源于stack exchange,提问作者Vaibhav Chavan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 19:15:39