Thanks for all the responses on this. The basic doubt I had was whether the JNDI lookup would create a new data-source on each lookup or just return the previously created one. A number of you had said that the latter was true. The reason I wasn't sure about that was because I'm creating C3P0 ComboPooledDataSource objects using the BeanFactory factory. And the docs here state that each lookup creates a new bean, which in my case is a new data-source: http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html#Generic%20 JavaBean%20Resources
I decided to test this by adding some logging in the various servlet init() methods to dump the C3P0 object returned from the lookup. The log output suggests that the same object is being returned back i.e. only one ComboPoolDataSource object is being used by both servlets. This is good for me. But it doesn't explain the discrepancy with the docs. Srinivas -----Original Message----- From: David Smith [mailto:[EMAIL PROTECTED] Sent: Friday, April 27, 2007 11:58 AM To: Tomcat Users List Subject: Re: Defining data-sources in tomcat But the code you posted does not create a new DataSource object. It just gets a reference to an existing one stored in JNDI. Nothing new about the DataSource object. Note the lack of a new operator. --David Rashmi Rubdi wrote: > On 4/27/07, albrecht andrzejewski <[EMAIL PROTECTED]> wrote: > >> >> >> 3) In my servlet init(), I lookup the data-source. Each lookup >> essentially >> >> instantiates an instance of the data-source. So a connection pool >> gets >> >> instantiated in each servlet's init. >> >> >> >> Now it doesn't make sense to create a new connection pool in each >> servlet. >> >> So I'd like to create a single connection pool that will be used >> by all >> >> servlets. I've been looking thru the tomcat docs, to see where I >> could >> >> create this central resource for my webapp. The only option I've >> seen so far >> >> is to create my own life-cycle listener for my webapp. That way I >> can create >> >> a connection pool on start-up. I was wondering if there was a >> better way to >> >> do this ? >> >> It's actually your container which create the connection pool and not >> your servlet. What you do when you are looking up is just finding a >> reference to the pre-existing datasource. So don't worry > > > Hi Ali, > > Thank you for the above post, I agree with it. > > Normally one has to write the following lines to get the database > connection from the connection pool: > > Context initContext = new InitialContext(); //(1) > Context envContext = > (Context)initContext.lookup("java:/comp/env"); //(2) > DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB"); > //(3) > conn = ds.getConnection(); //(4) > > I think most people try to initialize the DataSource object in the > above code, when Tomcat starts up and they only wan to get the ds > object in a DAO class before creating a new connection from the > connection pool. > > I think Srinivas wants to create the ds object when Tomcat starts up > and re-use the ds object in the DAO class instead of re-creating it > each time he needs it inside a DAO. > >> and please do >> not write your own lifecycle :-) >> >> Regards, Ali. > > > -Regards > Rashmi > > --------------------------------------------------------------------- > To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, > e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]