commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "G. Madhan Dennis" <>
Subject RE: DBCP and Servlet
Date Sun, 21 Mar 2004 09:18:16 GMT
Hi !

First of all if you use DBCP then there is no need to have a static
variable. When you webapp initializes (in your servlets init() method
for instance) for the first time do this:

String connectionURL = "<Your JDBC connection URL here>";
System.setProperty ("jdbc.drivers", "oracle.jdbc.driver.OracleDriver");
//Change this to your db's JDBC driver class name
ObjectPool connectionPool = new GenericObjectPool(null);
((GenericObjectPool) connectionPool).setMaxActive (10);
((GenericObjectPool) connectionPool).setMaxWait (1000 * 30); // 30 secs
((GenericObjectPool) connectionPool).setWhenExhaustedAction
ConnectionFactory connectionFactory = new
PoolableConnectionFactory poolableConnectionFactory = new
null,false, true);
PoolingDriver driver = new PoolingDriver();
driver.registerPool ("myconnectionpool", connectionPool);

Then where ever you need a connection (in your JSPs or servlets data
access methods for instance) call this:

        Connection conn = DriverManager.getConnection
//When you are done with the connection

Remember to be sure you call the close() method of the connection you
get from the driver manager. This method doesn't actually close the
connection to the db. It just returns the connection back to the DBCP
pool. However if you fail to call the close() the connection will never
be returned and you will soon run out of connections to retrieve from
the pool and No object available exceptions will be thrown.

You might also want to browse the API docs of the GenericObjectPool
class (Jakarta Commons - Pool) since it has many methods to configure
and fine-tune the pool to suit your needs.

Hope this helps :)


-----Original Message-----
From: Algirdas M. [] 
Sent: Saturday, March 20, 2004 10:56 PM
Subject: DBCP and Servlet


  I'm using DBCP in Servlets.

  My Connection from DataSource is a static variable.
  I need it static, because then I can get database connection
  everywhere in my objects.

  So, question is, should I close that static database connection
  after web page request end or not?
  I just want, that the database pooling worked correctly.


To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message