wood burning stoves 2.0*
The moose likes JDBC and the fly likes Commons-dbcp - getting the active database connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Commons-dbcp - getting the active database connections" Watch "Commons-dbcp - getting the active database connections" New topic

Commons-dbcp - getting the active database connections

Prachi Desai

Joined: Jan 12, 2011
Posts: 2
Hello All,

I have configured XA Datasource in JBOSS AS and the database is DB2. I want to get the number of active connections at any point of time for monitoring purpose. For that i tried using commons-dbcp. commons-dbcp has a BasicDataSource class which provides the API to get the actibve and idle connections.

Following is the code :

Context initialContext;
initialContext = new InitialContext();
DataSource ds = (DataSource) initialContext.lookup(myjndiname);
BasicDataSource bds = (BasicDataSource) datasource;
logger.info("************CONNECTION POOL DATA-> ACTIVE CONN=" + bds.getNumActive() + " IDLE CONN=" + bds.getNumIdle() + " INIT SIZE=" + bds.getInitialSize());

So when i try to cast my datasource object to basicDataSource, it gives ClassCast exception. I checked the class name of the datasource returned by initialcontext. It is returning WrapperDataSource and not Datasource and i guess that is the reason for classcastexception.

Can anyone pease tell me how to get the number of active connections using dbcp jar. I also cannot change the underlying db implementation as its a huge applicationa d a very old application.

Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30130

What datasource is it wrapping? Does it contain the basic data source?

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
wood burning stoves
subject: Commons-dbcp - getting the active database connections
Similar Threads
Connection Pooling Managment
Apache commons dbcp - SocketException: Too many open files
difference between Driver Manager and Data Source
DataSource and Driver Class loaded at runtime
How to pass arrays using JDBC to database?