• 监控c3p0的连接池


                SqlSession session = SessionFactory.getSqlSession(dbid);
                List<Map<String, Object>> resultList = session.getMapper(Mapper.class).getData(params);
                ConcurrentHashMap<String, SqlSessionFactory> sessionFactoryPool = SessionFactory.getSessionFactoryPool();
                
                Enumeration<String> keys = sessionFactoryPool.keys();
                while (keys.hasMoreElements())
                {
                    String dbid = (String) keys.nextElement();
    //                System.out.println("dbid : " + dbid);
                    SqlSessionFactory sqlSessionFactory = sessionFactoryPool.get(dbid);
                    Configuration configuration = sqlSessionFactory.getConfiguration();
                    Environment environment = configuration.getEnvironment();
                    DataSource dataSource = environment.getDataSource();
                    if (dataSource instanceof PooledDataSource)
                    {
                        PooledDataSource pds = (PooledDataSource) dataSource;
                        Collection allUsers = pds.getAllUsers();
                        System.out.println(allUsers);
                //获取总连接数 System.err.println(dbid
    + ": num_connections: " + pds.getNumConnectionsDefaultUser());
                //获取使用中的连接数 System.err.println(dbid
    + ": num_busy_connections: " + pds.getNumBusyConnectionsDefaultUser());
                //获取空闲连接数 System.err.println(dbid
    + ": num_idle_connections: " + pds.getNumIdleConnectionsDefaultUser());
                //获取未关闭的连接数 System.err.println(dbid
    + ": num_unclosed_connections: " + pds.getNumUnclosedOrphanedConnectionsDefaultUser()); System.err.println(); } else { System.err.println("Not a c3p0 PooledDataSource!"); } } return resultList;

    控制台输出结果

    dbid1: num_connections: 10
    dbid1: num_busy_connections: 0
    dbid1: num_idle_connections: 10
    dbid1: num_unclosed_connections: 0
    
    dbid2: num_connections: 10
    dbid2: num_busy_connections: 7
    dbid2: num_idle_connections: 3
    dbid2: num_unclosed_connections: 0
    
    dbid3: num_connections: 10
    dbid3: num_busy_connections: 0
    dbid3: num_idle_connections: 10
    dbid3: num_unclosed_connections: 0
  • 相关阅读:
    ios 视频旋转---分解ZFPlayer
    IOS lame库 pcm转mp3 分析(方案二)
    IOS lame库 pcm转mp3 分析(方案一)
    ios 动态库合成包(真机&模拟器)脚本
    lame 制作ios静态库
    React Native scrollview 循环播放
    React Native Image多种加载图片方式
    汉字转拼音(包含多音字)
    React Native Alert、ActionSheet
    React Native Picker (城市选择器)
  • 原文地址:https://www.cnblogs.com/hoonick/p/9883912.html
Copyright © 2020-2023  润新知