You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jim Richards <gr...@cyber4.org> on 2001/02/15 15:20:32 UTC

Re: AW: JDBC Connection Pooling

I'm trying to do the same thing here, but I'm not sure on a few
of the details.

I've got a servlet called DatabaseContainer, which will handle all
the database connections and populating Beans to send back
to the Actions.

What I can't work out is, once I've get the DataSource, how does the
Action know about the DatabaseContainer? At the moment I've
got 

        getServletContext().setAttribute("DatabaseContainer", this);

and so in the Action, I can call

	        DatabaseContainer database = (DatabaseContainer) servlet.getServletContext().getAttribute("DatabaseContainer");

But this doesn't seem right. Since Craig doesn't like singleton objects
hanging around, and I don't feel like I want to extend ActionServlet,
this seems like the only way (since getServlet() is depreciated in the
2.2 spec).

Any ideas?


>> The problem I'm facing is a bit diffrent as I want to access the
>> Connection Pool from a servlet that is started upon startup
>> of the servlet engine (similar to the struts example application).
>> In order to be able to access the DataSource, I tried to subclass
>> the ActionServlet class and to access the data source that way.
>> However, my servlet engine (Tomcat 3.2.1) complains that it is unable
>> to start this new Action servlet...
>>
>> Any ideas how to solve this problem?
>>
>There are three things you will need to do:
>
>* Make sure that both the Struts controller servlet and your own
>  servlet are marked <load-on-startup>.
>
>* Use values for the <load-on-startup> attribute that cause the
>  Struts controller servlet to be initialized first (for example, use a
>  "1" as the value for the controller servlet, and "2" for yours).
>  I'm not positive that Tomcat 3.2 respects this, but it is supposed to.
>
>* The Struts controller servlet makes the data source it initializes
>  available as a servlet context attribute, as well as available via a
>  method call.  You can therefore grab it by calling:
>
>    DataSource ds = (javax.sql.DataSource)
>      getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
>
>  in any other servlet (or JSP page) that is part of the same web app.



--
Kumera - a new Open Source Content Management System
for small to medium web sites written in Perl and using XML
http://www.cyber4.org/kumera/index.html

Re: AW: JDBC Connection Pooling

Posted by Alan Yackel <ay...@home.com>.
Jim,

I think you best bet is to actually extend ActionServlet.  I do something similar with
my application.  We use a different class instead of Datasource.  Just add in
a field for your container and get and set methods.  Then change the class
type in the web.xml.  You will then be able to get to your container from any
action class.

Alan Yackel

Jim Richards wrote:

> I'm trying to do the same thing here, but I'm not sure on a few
> of the details.
>
> I've got a servlet called DatabaseContainer, which will handle all
> the database connections and populating Beans to send back
> to the Actions.
>
> What I can't work out is, once I've get the DataSource, how does the
> Action know about the DatabaseContainer? At the moment I've
> got
>
>         getServletContext().setAttribute("DatabaseContainer", this);
>
> and so in the Action, I can call
>
>                 DatabaseContainer database = (DatabaseContainer) servlet.getServletContext().getAttribute("DatabaseContainer");
>
> But this doesn't seem right. Since Craig doesn't like singleton objects
> hanging around, and I don't feel like I want to extend ActionServlet,
> this seems like the only way (since getServlet() is depreciated in the
> 2.2 spec).
>
> Any ideas?
>
> >> The problem I'm facing is a bit diffrent as I want to access the
> >> Connection Pool from a servlet that is started upon startup
> >> of the servlet engine (similar to the struts example application).
> >> In order to be able to access the DataSource, I tried to subclass
> >> the ActionServlet class and to access the data source that way.
> >> However, my servlet engine (Tomcat 3.2.1) complains that it is unable
> >> to start this new Action servlet...
> >>
> >> Any ideas how to solve this problem?
> >>
> >There are three things you will need to do:
> >
> >* Make sure that both the Struts controller servlet and your own
> >  servlet are marked <load-on-startup>.
> >
> >* Use values for the <load-on-startup> attribute that cause the
> >  Struts controller servlet to be initialized first (for example, use a
> >  "1" as the value for the controller servlet, and "2" for yours).
> >  I'm not positive that Tomcat 3.2 respects this, but it is supposed to.
> >
> >* The Struts controller servlet makes the data source it initializes
> >  available as a servlet context attribute, as well as available via a
> >  method call.  You can therefore grab it by calling:
> >
> >    DataSource ds = (javax.sql.DataSource)
> >      getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
> >
> >  in any other servlet (or JSP page) that is part of the same web app.
>
> --
> Kumera - a new Open Source Content Management System
> for small to medium web sites written in Perl and using XML
> http://www.cyber4.org/kumera/index.html