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();
CodePudding user response:
You might try looking at getTotalNumberOfConnections() and subtracting getConnectionsAvailable() 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" or "javax.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.