Search code examples
javahibernatec3p0

Questions regarding C3P0 Pooled Data Source


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


Solution

  • 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

    1. for administrative reasons you like to keep close track of the number and status of all Connections your application is using;

    2. 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;

    3. to work around problems that may occur if an underlying jdbc driver / DBMS system is unreliable. .

    also There is description on method names available in javadoc.

    see Method Names ... section

    Many methods in this interface have three variants:

    1. < method-name> DefaultUser()
    2. < method-name> (String username, String password)
    3. < method-name> AllUsers()

    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.