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
(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
ConnectionFactory connectionFactory = new
DriverManagerConnectionFactory(connectionURL,null);
PoolableConnectionFactory poolableConnectionFactory = new
PoolableConnectionFactory(connectionFactory,connectionPool,null,
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
("jdbc:apache:commons:dbcp:myconnectionpool");
.
.
.
.
//When you are done with the connection
        conn.close();


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 :)

Regards,
Madhan


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

Hello,

  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.


  Thanks,
   snk


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to