tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <Yoav.Shap...@mpi.com>
Subject RE: OT: db pool problems - jdbc connection count?
Date Thu, 30 Oct 2003 20:40:44 GMT

Howdy,
It's very similar to how you get a connection.  Let's say your
datasource is defined in server.xml with name "jdbc/myDataSource".
Then:

InitialContext ic = new InitialContext();
DataSource ds = (DataSource)
ic.lookup("java:comp/env/jdbc/myDataSource");

If you wanted a connection, you'd call ds.getConnection().  But you want
to find the number of active connections, which is not part of the
DataSource interface.  So you need to cast ds to the specific type,
which is the type your specified in server.xml.

Assuming that type is org.apache.commons.dbcp.BasicDataSource, you'd do:
BasicDataSourcs bds = (BasicDataSource) ds;
int numActive = bds.getNumActive();
int numIdle = bds.getNumIdle();
System.out.println("There are " + numActive + 
" active connections and " + numIdle + 
" idle connections in the pool.");

I'm assuming you're using DBCP.  If you're using another connection
pool, the above code must be modified accordingly.

Yoav Shapira
Millennium ChemInformatics


>-----Original Message-----
>From: john-paul delaney [mailto:jp@justatest.com]
>Sent: Thursday, October 30, 2003 6:30 PM
>To: Tomcat Users List
>Subject: RE: OT: db pool problems - jdbc connection count?
>
>Thanks Yoav... sorry it's a wee bit above my immediate understanding
(e.g
>lookup my connection pool by JNDI name - I could do with an example of
what
>this means) plese be patient and point me to a resource that will give
me
>some lead to catch up with you.
>
>
>regards
>/j-p.
>
>
>On Thu, 30 Oct 2003, Shapira, Yoav wrote:
>>
>> Howdy,
>> You can write a bit of code to do this:
>>
>> - Create an InitialContext
>> - Get the java:comp/env context
>> - Lookup your connection pool by JNDI name
>> - Cast your connection pool to the appropriate type as specified in
your
>> server.xml
>>
>> Then you can call the implementation-specific method, e.g.
>> getNumActive() / getNumIdle() for
>> org.apache.commons.dbcp.BasicDataSource, to look at values.
>>
>> Yoav Shapira
>> Millennium ChemInformatics
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org




This e-mail, including any attachments, is a confidential business communication, and may
contain information that is confidential, proprietary and/or privileged.  This e-mail is intended
only for the individual(s) to whom it is addressed, and may not be saved, copied, printed,
disclosed or used by anyone else.  If you are not the(an) intended recipient, please immediately
delete this e-mail from your computer system and notify the sender.  Thank you.


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message