如何在Eclipselink中获取JPA连接池统计信息?
我目前正在使用 eclipselink JPA 从事Dropwizard框架项目。我需要获取连接池统计信息,例如活动连接的数量,连接总数等。我尝试使用以下方法,但它总是显示相同的数据。请帮助我解决这个问题。
EntityManager entityManager = (EntityManager) unitOfWorkI.getContext();
ServerSession serverSession = entityManager.unwrap(ServerSession.class);
for (Map.Entry<String, ConnectionPool> entry: serverSession.getConnectionPools().entrySet()) {
System.out.println("Connection Pool Name: " + entry.getKey());
System.out.println("Min number of Connections: " + entry.getValue().getMinNumberOfConnections());
System.out.println("Max number of Connections: " + entry.getValue().getMaxNumberOfConnections());
System.out.println("Connections in use: " + (entry.getValue().getMaxNumberOfConnections() - entry.getValue().getConnectionsAvailable().size()));
}
entityManager.close();
I am currently working on Dropwizard framework project with EclipseLink JPA. I need to get connection pool statistics such as the number of active connections, the total number of connections, etc. I tried with the below method, but it always shows the same data. Please help me in solving this problem.
EntityManager entityManager = (EntityManager) unitOfWorkI.getContext();
ServerSession serverSession = entityManager.unwrap(ServerSession.class);
for (Map.Entry<String, ConnectionPool> entry: serverSession.getConnectionPools().entrySet()) {
System.out.println("Connection Pool Name: " + entry.getKey());
System.out.println("Min number of Connections: " + entry.getValue().getMinNumberOfConnections());
System.out.println("Max number of Connections: " + entry.getValue().getMaxNumberOfConnections());
System.out.println("Connections in use: " + (entry.getValue().getMaxNumberOfConnections() - entry.getValue().getConnectionsAvailable().size()));
}
entityManager.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试查看
getTotalNumberOfConnections()
和减去getConnectionsAvailable()
,它应该为您提供池中使用的连接数量。根据游泳池的不同,您不应该看到太多的变化。一般而言,读取连接和连接从池中懒洋洋地获取,以确保它们不会比必要的时间更长,因此您的投票必须非常频繁才能捕捉使用。您可以查看类的来源 connectionpool.java 和 readconnectionpool.java 要了解要寻找和使用的内容,您也可能会查看 eclipselink性能Profiler 但是您可能可以说在可能有助于收集连接统计的领域投资不多。我相信这样的想法是,大多数寻找这些选项都会选择一个外部数据源进行汇总。我建议您考虑使用或其他一些池,其中内置了大量统计跟踪机制。
通过在您自己的实例中,您可以使用
jakarta.persistence.nonjtadatasource
或javax.persistence.persistence.nonjtadatatasource
persistence属性直接在实例中传递,或者在您的名字中给出一个名称在jndi中可以查找的容器环境中。You might try looking at
getTotalNumberOfConnections()
and subtractinggetConnectionsAvailable()
which should give you the number of connections in use on a pool. Depending on the pool, you shouldn't see too much change; read connections and connections in general are lazily fetched from pools to ensure they are not held on longer than necessary, so your polling would have to be very frequent to catch usage.You can take a look at the source for the classes ConnectionPool.java and ReadConnectionPool.java to get an idea of what to look for and use, and you might also look at the EclipseLink performance profiler but you can probably tell there isn't a lot of investment in areas that might help gather connection statistics. I believe the thinking was that most looking for those options would pick an external datasource for pooling; I'd suggest you look into using Oracle UCP or some other pool that EclipseLink can use which has plenty of statistics tracking mechanisms built in.
To pass in your own instance, you can use the
jakarta.persistence.nonJtaDataSource
orjavax.persistence.nonJtaDataSource
persistence property to pass in the instance directly, or give a name if you are in a container environment that EclipseLink can look up in JNDI.