You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Srinivas V." <sr...@fastmail.fm> on 2007/04/27 19:22:07 UTC

Defining data-sources in tomcat

Hello all

I have a question about defining data-sources in tomcat. I have a webapp in
which my servlets need to talk to a DB. This is my current setup:

1) I define data-sources (c3p0 connection pools) using the Resource tag in
my META-INF/context.xml.
2) The web.xml has references these data-sources via resource-env-ref tags
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 ?

       Thanks

      Srinivas

Re: Defining data-sources in tomcat

Posted by Rashmi Rubdi <ra...@gmail.com>.
On 4/27/07, Srinivas V. <sr...@fastmail.fm> wrote:
> Hello all
>
> I have a question about defining data-sources in tomcat. I have a webapp in
> which my servlets need to talk to a DB. This is my current setup:
>
> 1) I define data-sources (c3p0 connection pools) using the Resource tag in
> my META-INF/context.xml.
> 2) The web.xml has references these data-sources via resource-env-ref tags
> 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've seen people using ServletContextListener instead of
LifeCycleListener. That way you don't have to write your own listener.

contextInitialized() method is invoked when Tomcat starts or re-loads.

Reference: http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html

You may already know that the listener must be registered in web.xml
under a <listener> tag.

> I was wondering if there was a better way to
> do this ?
>
>        Thanks
>
>       Srinivas
>

-Regards
Rashmi

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining data-sources in tomcat

Posted by Rashmi Rubdi <ra...@gmail.com>.
On 4/27/07, Srinivas V. <sr...@fastmail.fm> wrote:
> 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.

Hi Srinivas,

I don't see any discrepancy based on David's point in the above post:
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");  , doesn't
create a new
bean.


FYI: I refer to this doc:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
 specifically for JDBC Connection pooling , instead of the above link
you referenced as it closely concerns with JDBC and not general JNDI.
But both documents are good.

>
>        Srinivas

-Regards
Rashmi

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining data-sources in tomcat

Posted by Rashmi Rubdi <ra...@gmail.com>.
On 4/27/07, David Smith <dn...@cornell.edu> wrote:
> 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.

Good post, thank you for pointing this.

The only new operator concerning the snippet that obtains a ds object
I see in my code is:
Context initContext = new InitialContext(); //(1)

Just to clarify, the consensus from this thread is that there's no
need to create a DataSource object when Tomcat starts up.

Regards
-Rashmi

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Defining data-sources in tomcat

Posted by "Srinivas V." <sr...@fastmail.fm>.
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:dns4@cornell.edu] 
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 <al...@ema.fr> 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: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe,
e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining data-sources in tomcat

Posted by David Smith <dn...@cornell.edu>.
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 <al...@ema.fr> 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: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining data-sources in tomcat

Posted by Rashmi Rubdi <ra...@gmail.com>.
On 4/27/07, albrecht andrzejewski <al...@ema.fr> 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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining data-sources in tomcat

Posted by albrecht andrzejewski <al...@ema.fr>.
>> 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 and please do  
not write your own lifecycle :-)

Regards, Ali.

----------------------------------------------------
Ce message a ete envoye par le serveur IMP de l'EMA.



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Defining data-sources in tomcat

Posted by David Smith <dn...@cornell.edu>.
I don't think you are creating new database pools on each servlet init.  
Looks like you are just getting another reference to the DataSource 
object that's stored in the JNDI.  Can you post the code you use in 
servlet.init() to get the DataSource?

--David

Srinivas V. wrote:

>Hello all
>
>I have a question about defining data-sources in tomcat. I have a webapp in
>which my servlets need to talk to a DB. This is my current setup:
>
>1) I define data-sources (c3p0 connection pools) using the Resource tag in
>my META-INF/context.xml.
>2) The web.xml has references these data-sources via resource-env-ref tags
>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 ?
>
>       Thanks
>
>      Srinivas
>
>  
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org