Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 48231 invoked from network); 21 Mar 2004 09:31:23 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 21 Mar 2004 09:31:23 -0000 Received: (qmail 21452 invoked by uid 500); 21 Mar 2004 09:30:53 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 21431 invoked by uid 500); 21 Mar 2004 09:30:53 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 21415 invoked from network); 21 Mar 2004 09:30:53 -0000 Received: from unknown (HELO mail-sg.isoftel.com) (202.42.37.36) by daedalus.apache.org with SMTP; 21 Mar 2004 09:30:53 -0000 Received: from EXCH-SG.isoftel.ad (unverified) by mail-sg.isoftel.com (Content Technologies SMTPRS 4.3.12) with ESMTP id ; Sun, 21 Mar 2004 17:32:41 +0800 Received: from arun ([203.197.157.82]) by EXCH-SG.isoftel.ad with Microsoft SMTPSVC(5.0.2195.6713); Sun, 21 Mar 2004 17:15:51 +0800 From: "G. Madhan Dennis" To: "'Jakarta Commons Users List'" , "'Algirdas M.'" Subject: RE: DBCP and Servlet Date: Sun, 21 Mar 2004 14:48:16 +0530 Message-ID: <000701c40f25$74e57d60$8c0010ac@amoebatel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Importance: Normal In-Reply-To: <18314491888.20040320182531@splius.lt> X-OriginalArrivalTime: 21 Mar 2004 09:15:53.0259 (UTC) FILETIME=[1C156FB0:01C40F25] X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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 = ""; 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:snk@splius.lt] Sent: Saturday, March 20, 2004 10:56 PM To: commons-user@jakarta.apache.org 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: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org