Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 929
Umer Hayat Avatar asked Sep 23 '11 04:09

Umer Hayat


People also ask

How do I monitor my c3p0 connection pool?

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.

How does c3p0 connection pooling work?

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.

What is get pooled connection from DataSource?

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.

What is the difference between DataSource and connection 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.


1 Answers

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.

like image 161
swanliu Avatar answered Oct 25 '22 07:10

swanliu