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