You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Craig R. McClanahan" <cr...@apache.org> on 2004/05/02 06:59:32 UTC

Re: Database access in contextInitialized

Samuel Rochas wrote:

> Hello Struts Gurus,
>
> I am starting with struts.
> I used to make some initialization in my Servlets using the init() 
> method. With struts, I just implemented the contextInitialized method 
> of ServletContextListener Interface, and I have a place for such 
> initializations. Fine.
>
> The things I used to initialize were some Collections with data from a 
> database. With struts, I am now using the datasource configuration in 
> struts-config.xml and I access it in my Actions with: 
> getDataSource(request). Fine too.
>
> But, I don't have access to getDataSource() in my 
> ServletContextListener implementation. So how can I access my database 
> there, the same way I access it in the rest of the application? 


The problem, as you've undoubtedly discovered, is that your 
ServletContextListener gets invoked before the ActionServlet.init() 
method has had a chance to run, so the Struts initialization has not yet 
been performed.  This makes perfect sense when you understand that 
Struts was created to run on top of Servlet 2.2, before there was such a 
thing as a ServletContextListener.

Indeed, the recommended pattern today is for you to acquire your 
DataSource instances from the JNDI naming context (configured by your 
app server's admin console), rather than configuring them inside the 
webapp.  The getDataSource() method, and everything about 
Struts-provided connection pools, has been deprecated but remains only 
for backwards compatibility purposes.

If it is too painful to switch to JNDI-provided data sources in the 
short run, then you should do your initialization in a Struts PlugIn 
instead of using a ServletContextListener.  These are executed after the 
Struts-defined data sources have been created, so they will be available 
to you during your initialization.

But you should really really switch to JNDI-provided data sources if 
they are supported by your container.

Craig McClanahan



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Database access in contextInitialized

Posted by Richard Yee <ry...@cruzio.com>.
Craig,
When I put logging statements in the ServletContextInitializer class, I see 
that the ServletContextInitializer.contextInitialized() method is actually 
called after the servlet init() methods for servlets that load at startup. 
Is this correct? You say that the contextInitialized() gets called before 
the ActionServlet.init() method is called.

Thanks,

Richard


At 09:59 PM 5/1/2004, you wrote:
>Samuel Rochas wrote:
>
>>Hello Struts Gurus,
>>
>>I am starting with struts.
>>I used to make some initialization in my Servlets using the init() 
>>method. With struts, I just implemented the contextInitialized method of 
>>ServletContextListener Interface, and I have a place for such 
>>initializations. Fine.
>>
>>The things I used to initialize were some Collections with data from a 
>>database. With struts, I am now using the datasource configuration in 
>>struts-config.xml and I access it in my Actions with: 
>>getDataSource(request). Fine too.
>>
>>But, I don't have access to getDataSource() in my ServletContextListener 
>>implementation. So how can I access my database there, the same way I 
>>access it in the rest of the application?
>
>
>The problem, as you've undoubtedly discovered, is that your 
>ServletContextListener gets invoked before the ActionServlet.init() method 
>has had a chance to run, so the Struts initialization has not yet been 
>performed.  This makes perfect sense when you understand that Struts was 
>created to run on top of Servlet 2.2, before there was such a thing as a 
>ServletContextListener.
>
>Indeed, the recommended pattern today is for you to acquire your 
>DataSource instances from the JNDI naming context (configured by your app 
>server's admin console), rather than configuring them inside the 
>webapp.  The getDataSource() method, and everything about Struts-provided 
>connection pools, has been deprecated but remains only for backwards 
>compatibility purposes.
>
>If it is too painful to switch to JNDI-provided data sources in the short 
>run, then you should do your initialization in a Struts PlugIn instead of 
>using a ServletContextListener.  These are executed after the 
>Struts-defined data sources have been created, so they will be available 
>to you during your initialization.
>
>But you should really really switch to JNDI-provided data sources if they 
>are supported by your container.
>
>Craig McClanahan
>
>
>
>---------------------------------------------------------------------
>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: Database access in contextInitialized

Posted by Samuel Rochas <sa...@andinasoft.com>.
Hello Craig,

> Indeed, the recommended pattern today is for you to acquire your 
> DataSource instances from the JNDI naming context
Thanks for your advise. I was the way I handled it before using struts, 
so I will continue that way.

Sincerly
Samuel

---  andinasoft SA - Software y Consulting  ---
Mariano Aguilera 276 y Almagro - Quito, Ecuador
Tel. +593 2 290 55 18 ---- Cel. +593 9 946 4046
---------  http://www.andinasoft.com  ---------


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org