You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Dipole Moment <di...@gmail.com> on 2005/03/07 13:33:43 UTC
[DBCP] Servlets sharing a DataSource
Hi all,
I've been researching what's the best way of sharing a DataSource
between servlets with DBCP, and I've come up with the following
scenario: I implement a ServletContextListener and obtain a
DataSource in contextInitialized() method and put it as an attribute
in ServletContext
Context env = null;
DataSource pool;
env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource) env.lookup("jdbc/postgres");
context.setAttribute("POOL", pool);
and then every servlet that wants to execute a query, looks up the
POOL attribute and does a getConnection() on it, creates a statement
and does its job...
pool = (DataSource) context.getAttribute("POOL");
conn = pool.getConnection();
stmt = conn.createStatement();
...
My questions are as follows: is the above usage valid, thread-safe?
Are there any better ways of doing it? And in case of DataSource, I
could not find a method for it that closes it so that I could call it
in contextDestroyed() method of the ServletContextListener, is it ok
to shutdown application without doing any sort of a "close()" on
DataSource?
Thanks!
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: [DBCP] Servlets sharing a DataSource
Posted by Dipole Moment <di...@gmail.com>.
Well, I'm not using Struts, but I'll take a look at the Struts source
to see how it's doing it.
Thanks!
On Mon, 7 Mar 2005 18:15:05 +0530, Sanjeet Joshi <sa...@gmail.com> wrote:
> Hi,
>
> Just get it everytime you need it by calling Action.getDataSource(..)
> because Struts framework itself stores it in the ServletContext. Don't
> repeat it again in you code.
>
> Cheers
> Sanjeet
>
>
> On Mon, 7 Mar 2005 14:33:43 +0200, Dipole Moment
> <di...@gmail.com> wrote:
> > Hi all,
> >
> > I've been researching what's the best way of sharing a DataSource
> > between servlets with DBCP, and I've come up with the following
> > scenario: I implement a ServletContextListener and obtain a
> > DataSource in contextInitialized() method and put it as an attribute
> > in ServletContext
> >
> > Context env = null;
> > DataSource pool;
> > env = (Context) new InitialContext().lookup("java:comp/env");
> > pool = (DataSource) env.lookup("jdbc/postgres");
> > context.setAttribute("POOL", pool);
> >
> > and then every servlet that wants to execute a query, looks up the
> > POOL attribute and does a getConnection() on it, creates a statement
> > and does its job...
> >
> > pool = (DataSource) context.getAttribute("POOL");
> > conn = pool.getConnection();
> > stmt = conn.createStatement();
> > ...
> >
> > My questions are as follows: is the above usage valid, thread-safe?
> > Are there any better ways of doing it? And in case of DataSource, I
> > could not find a method for it that closes it so that I could call it
> > in contextDestroyed() method of the ServletContextListener, is it ok
> > to shutdown application without doing any sort of a "close()" on
> > DataSource?
> >
> > Thanks!
> >
> > ---------------------------------------------------------------------
> > 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
Re: [DBCP] Servlets sharing a DataSource
Posted by Sanjeet Joshi <sa...@gmail.com>.
Hi,
Just get it everytime you need it by calling Action.getDataSource(..)
because Struts framework itself stores it in the ServletContext. Don't
repeat it again in you code.
Cheers
Sanjeet
On Mon, 7 Mar 2005 14:33:43 +0200, Dipole Moment
<di...@gmail.com> wrote:
> Hi all,
>
> I've been researching what's the best way of sharing a DataSource
> between servlets with DBCP, and I've come up with the following
> scenario: I implement a ServletContextListener and obtain a
> DataSource in contextInitialized() method and put it as an attribute
> in ServletContext
>
> Context env = null;
> DataSource pool;
> env = (Context) new InitialContext().lookup("java:comp/env");
> pool = (DataSource) env.lookup("jdbc/postgres");
> context.setAttribute("POOL", pool);
>
> and then every servlet that wants to execute a query, looks up the
> POOL attribute and does a getConnection() on it, creates a statement
> and does its job...
>
> pool = (DataSource) context.getAttribute("POOL");
> conn = pool.getConnection();
> stmt = conn.createStatement();
> ...
>
> My questions are as follows: is the above usage valid, thread-safe?
> Are there any better ways of doing it? And in case of DataSource, I
> could not find a method for it that closes it so that I could call it
> in contextDestroyed() method of the ServletContextListener, is it ok
> to shutdown application without doing any sort of a "close()" on
> DataSource?
>
> Thanks!
>
> ---------------------------------------------------------------------
> 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
RE: [DBCP] Servlets sharing a DataSource
Posted by Daniel Huang <da...@cisco.com>.
If you are using Tomcat, you can config your DataSource in server.xml. Take
a look at
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-how
to.html
Thanks
Daniel
-----Original Message-----
From: Dipole Moment [mailto:dipole.moment@gmail.com]
Sent: Monday, March 07, 2005 4:34 AM
To: commons-user@jakarta.apache.org
Subject: [DBCP] Servlets sharing a DataSource
Hi all,
I've been researching what's the best way of sharing a DataSource
between servlets with DBCP, and I've come up with the following
scenario: I implement a ServletContextListener and obtain a
DataSource in contextInitialized() method and put it as an attribute
in ServletContext
Context env = null;
DataSource pool;
env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource) env.lookup("jdbc/postgres");
context.setAttribute("POOL", pool);
and then every servlet that wants to execute a query, looks up the
POOL attribute and does a getConnection() on it, creates a statement
and does its job...
pool = (DataSource) context.getAttribute("POOL");
conn = pool.getConnection();
stmt = conn.createStatement();
...
My questions are as follows: is the above usage valid, thread-safe?
Are there any better ways of doing it? And in case of DataSource, I
could not find a method for it that closes it so that I could call it
in contextDestroyed() method of the ServletContextListener, is it ok
to shutdown application without doing any sort of a "close()" on
DataSource?
Thanks!
---------------------------------------------------------------------
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