I tried to use Pooled Data source to log information regarding database connection pool i.e, Max pool size, current no. of connections in use, busy connection etc. I am using C3P0Registry to get pooled data source.
PooledDataSource dataSource =null;
try{
C3P0Registry.getNumPooledDataSources();
//I am sure that I am using only one data source
Iterator<Set> connectionIterator = C3P0Registry.getPooledDataSources().iterator();
dataSource = (PooledDataSource)connectionIterator.next();
}catch (Exception e) {
}
and then i am logging required information as:
Logger.write(LoggerConstant.DEBUG, " Connections in use: "+dataSource.getNumConnectionsAllUsers()+" , Busy Connections: "+dataSource.getNumBusyConnectionsAllUsers() +" , Idle Connections: "+ dataSource.getNumIdleConnectionsAllUsers()+" , Unclosed Orphaned Connections: "+ dataSource.getNumUnclosedOrphanedConnectionsAllUsers(), methodName);
I want to know that if its a correct way to achieve my goal?.
Plus i am having confusions regarding What does dataSource.getNumConnectionsAllUsers() and other function (i am using) exactly return. There is no description available in javadoc.
Is there any description or may be tutorial available online from where i can learn more about these particular functions?
Environment: Java, Hibernate, C3P0, MySQL
If JMX is enabled in your application environment, the best way to monitor connection pool is through MBeanServer with the help of a tool such as Jconsole, VisualVM. In case of VisualVM, you will need to install an additional plugin called “MBean” in order to be able to monitor C3p0 datasources.
Connection Pooling with the c3p0 Libraryc3p0 is an easy-to-use library for making traditional JDBC drivers “enterprise-ready” by augmenting them with functionality defined by the jdbc3 spec and the optional extensions to jdbc2. As of version 0.9. 5, c3p0 fully supports the jdbc4 spec.
Getting and Using Pooled Connections. A connection pool is a cache of database connection objects. The objects represent physical database connections that can be used by an application to connect to a database. At run time, the application requests a connection from the pool.
DataSource objects that implement connection pooling also produce a connection to the particular data source that the DataSource class represents. The connection object that the getConnection method returns is a handle to a PooledConnection object rather than being a physical connection.
try read PooledDataSource java doc. http://www.mchange.com/projects/c3p0/apidocs/com/mchange/v2/c3p0/PooledDataSource.html
PooledDataSource.getXXXXUser() is correct way of monitor and manage data source
The functionality in this interface will be only be of interest if
for administrative reasons you like to keep close track of the number and status of all Connections your application is using;
to work around problems encountered while managing a DataSource whose clients are poorly coded applications that leak Connections, but which you are not permitted to fix;
also There is description on method names available in javadoc.
see Method Names ... section
Many methods in this interface have three variants:
The first variant makes use of the pool maintained for the default user -- Connections created by calls to the no argument getConnection(), the second variant lets you keeps track of pools created by calling getConnection( username, password ), and the third variant provides aggregate information or performs operation on all pools.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With