struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Richard Yee <r...@cruzio.com>
Subject Re: Database access in contextInitialized
Date Mon, 03 May 2004 01:50:29 GMT
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


Mime
View raw message