Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Commons-dbcp - getting the active database connections

 
Prachi Desai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35976
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What datasource is it wrapping? Does it contain the basic data source?
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!