You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Matt Hughes <mh...@feith.com> on 2004/10/20 19:28:32 UTC
Getting DataSource from non-Struts class
Hi,
I am developing a web service for my application and am putting the
class in the same web context as the rest of my struts application.
However, I want to be able to get a DataSource object from within this
class. The only thing I have to connect the two is a ServletContext
object. How can I call Struts or get a hold of this DataSource object
when I am not in an Action class, etc.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Getting DataSource from non-Struts class
Posted by Sean Schofield <se...@gmail.com>.
I agree with this. We use Tomcat to set up the connection pool and
reference it in our Struts apps as Craig suggests above. Take a look
at the Tomcat documentation, there is quite a bit on JNDI and
datasources.
Good luck,
sean
On Wed, 20 Oct 2004 11:00:11 -0700, Craig McClanahan <cr...@gmail.com> wrote:
> More modern advice suggests that you don't use a Struts-provided
> connection pool at all ... use the JNDI factilities provided by your
> servlet container or app server. Then, you gain access to a
> connection pool provided by the server, like this:
>
> InitialContext ic = new InitialContext();
> DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/CustomerDB");
>
> without having to pass any parameters around.
>
> Craig
>
> On Wed, 20 Oct 2004 13:36:27 -0400, dgraham@evergreeninvestments.com
>
>
> <dg...@evergreeninvestments.com> wrote:
> >
> > Found this at http://www2.real-time.com/rte-tomcat/2000/Jun/msg01487.html
> > --
> >
> > The suggested approach is to store your connection pool object itself as
> > a servlet context attribute, like this:
> >
> > ConnectionPool pool = new ConnectionPool( ... );
> > getServletContext().setAttribute("pool", pool);
> >
> > Now, any servlet (or JSP page) in this web application can gain access
> > to the connection pool:
> >
> > ConnectionPool pool = (ConnectionPool)
> > getServletContext().getAttribute("pool");
> >
> > without needing to gain access to a servlet instance itself.
> >
> > Craig McClanahan
> >
> >
> >
> >
> > Matt Hughes <mh...@feith.com>
> >
> > 10/20/2004 01:28 PM
> >
> > Please respond to
> > "Struts Users Mailing List" <us...@struts.apache.org>
> >
> >
> > To "'Struts Users Mailing List'" <us...@struts.apache.org>
> >
> > cc
> >
> > Subject Getting DataSource from non-Struts class
> >
> >
> >
> >
> >
> >
> >
> > Hi,
> >
> > I am developing a web service for my application and am putting the
> > class in the same web context as the rest of my struts application.
> > However, I want to be able to get a DataSource object from within this
> > class. The only thing I have to connect the two is a ServletContext
> > object. How can I call Struts or get a hold of this DataSource object
> > when I am not in an Action class, etc.
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
> ---------------------------------------------------------------------
>
>
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Getting DataSource from non-Struts class
Posted by Craig McClanahan <cr...@gmail.com>.
More modern advice suggests that you don't use a Struts-provided
connection pool at all ... use the JNDI factilities provided by your
servlet container or app server. Then, you gain access to a
connection pool provided by the server, like this:
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/CustomerDB");
without having to pass any parameters around.
Craig
On Wed, 20 Oct 2004 13:36:27 -0400, dgraham@evergreeninvestments.com
<dg...@evergreeninvestments.com> wrote:
>
> Found this at http://www2.real-time.com/rte-tomcat/2000/Jun/msg01487.html
> --
>
> The suggested approach is to store your connection pool object itself as
> a servlet context attribute, like this:
>
> ConnectionPool pool = new ConnectionPool( ... );
> getServletContext().setAttribute("pool", pool);
>
> Now, any servlet (or JSP page) in this web application can gain access
> to the connection pool:
>
> ConnectionPool pool = (ConnectionPool)
> getServletContext().getAttribute("pool");
>
> without needing to gain access to a servlet instance itself.
>
> Craig McClanahan
>
>
>
>
> Matt Hughes <mh...@feith.com>
>
> 10/20/2004 01:28 PM
>
> Please respond to
> "Struts Users Mailing List" <us...@struts.apache.org>
>
>
> To "'Struts Users Mailing List'" <us...@struts.apache.org>
>
> cc
>
> Subject Getting DataSource from non-Struts class
>
>
>
>
>
>
>
> Hi,
>
> I am developing a web service for my application and am putting the
> class in the same web context as the rest of my struts application.
> However, I want to be able to get a DataSource object from within this
> class. The only thing I have to connect the two is a ServletContext
> object. How can I call Struts or get a hold of this DataSource object
> when I am not in an Action class, etc.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Getting DataSource from non-Struts class
Posted by DG...@EvergreenInvestments.com.
Found this at http://www2.real-time.com/rte-tomcat/2000/Jun/msg01487.html
--
The suggested approach is to store your connection pool object itself as
a servlet context attribute, like this:
ConnectionPool pool = new ConnectionPool( ... );
getServletContext().setAttribute("pool", pool);
Now, any servlet (or JSP page) in this web application can gain access
to the connection pool:
ConnectionPool pool = (ConnectionPool)
getServletContext().getAttribute("pool");
without needing to gain access to a servlet instance itself.
Craig McClanahan
Matt Hughes <mh...@feith.com>
10/20/2004 01:28 PM
Please respond to
"Struts Users Mailing List" <us...@struts.apache.org>
To
"'Struts Users Mailing List'" <us...@struts.apache.org>
cc
Subject
Getting DataSource from non-Struts class
Hi,
I am developing a web service for my application and am putting the
class in the same web context as the rest of my struts application.
However, I want to be able to get a DataSource object from within this
class. The only thing I have to connect the two is a ServletContext
object. How can I call Struts or get a hold of this DataSource object
when I am not in an Action class, etc.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org