Return-Path: Delivered-To: apmail-jakarta-tomcat-user-archive@www.apache.org Received: (qmail 48916 invoked from network); 3 Dec 2003 10:41:28 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 3 Dec 2003 10:41:28 -0000 Received: (qmail 85951 invoked by uid 500); 3 Dec 2003 10:40:42 -0000 Delivered-To: apmail-jakarta-tomcat-user-archive@jakarta.apache.org Received: (qmail 85930 invoked by uid 500); 3 Dec 2003 10:40:42 -0000 Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Tomcat Users List" Reply-To: "Tomcat Users List" Delivered-To: mailing list tomcat-user@jakarta.apache.org Received: (qmail 85914 invoked from network); 3 Dec 2003 10:40:42 -0000 Received: from unknown (HELO smtpout.mac.com) (17.250.248.84) by daedalus.apache.org with SMTP; 3 Dec 2003 10:40:42 -0000 Received: from mac.com (smtpin07-en2 [10.13.10.152]) by smtpout.mac.com (8.12.6/MantshX 2.0) with ESMTP id hB3AeuDC004073 for ; Wed, 3 Dec 2003 02:40:56 -0800 (PST) Received: from [192.168.1.100] (12-220-218-65.client.insightbb.com [12.220.218.65]) (authenticated bits=0) by mac.com (Xserve/smtpin07/MantshX 3.0) with ESMTP id hB3AetaN019389 for ; Wed, 3 Dec 2003 02:40:55 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v606) In-Reply-To: <3FCD97D6.40204@ev.co.yu> References: <67A8A2CC-253F-11D8-BEC7-0030656EC0E4@mac.com> <200312032022.56558.peter.harrison@nothingbutnet.co.nz> <3FCD97D6.40204@ev.co.yu> Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <25973DCA-257D-11D8-A01C-0030656EC0E4@mac.com> Content-Transfer-Encoding: 7bit From: "Todd O'Bryan" Subject: Re: Servlets with JDBC connectivity Date: Wed, 3 Dec 2003 05:40:43 -0500 To: Tomcat Users List X-Mailer: Apple Mail (2.606) 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 On Dec 3, 2003, at 2:59 AM, Nikola Milutinovic wrote: > Peter Harrison wrote: >> On Wed, 03 Dec 2003 16:18, Todd O'Bryan wrote: >>> How do people handle this elegantly? The requirements are: a single, >>> globally visible (within a webapp) database interface and the ability >>> to access multiple databases easily. >> The first point is to use a singleton to set up the database >> connection - or more correctly the connection pool. This way you can >> request a connection and return it to the pool easily. Of course >> every time you use one you will have to use try-catch blocks. Sorry >> no way around that. > > Both Tomcat and J2EE specification support javax.sql.DataSource > objects over JNDI. That is how we handle it elegantly. > > A DataSource object is specified by the administrator and created by > the container. Container then deploys it under specified JNDI name. A > servlet (or EJB) can then lookup this object and use it, something > like this: > > import java.sql.*; > import javax.sql.*; > import javax.naming.*; > > InitialContext ic = new InitialContext(); > DataSource ds = (DataSource)ic.lookup( > "java:comp/env/jdbc/MyDataSource" ); > Connection conn = ds.getConnection(); > But this means I still have to get a connection, create a statement, and execute a query or update on the statement in every servlet where I want to use the connection. Yes, it locates the connection details (i.e., the JDBC connection method, the database name, user and password) somewhere centrally so that I don't have to keep coding it, but all of the connection overhead still has to be dealt with in every servlet in a webapp. At least, I think that's what it's doing. Am I missing something? Here's what I want. In every servlet in my webapp, I'm only going to be using one of a very few database connections. I'd like to be able to do something like: Test.executeQuery("SQL") without doing any setup because the Test class handles all the setup, initializes the connection the first time it's called, etc. Basically, it's a singleton class that never gets an instance created because if I create an instance, I'd have to pass it around to all the servlets in the webapp, which would kill the convenience of having it be available with minimal overhead. Oo, oo, oo... As I've been sitting here writing this, I had a brainstorm. What if I create an abstract class that has all the database connectivity built into it as static methods, but is just waiting for a connection method, a username, and a password. Then creating a new JDBC connection which is visible to all my servlets is just a matter of making a concrete subclass that defines those three variables and inherits all the functionality from its abstract parent class. Any reason this is a horrible idea? Thanks for being patient, Todd --------------------------------------------------------------------- To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: tomcat-user-help@jakarta.apache.org