PDA

View Full Version : Issue: Spring initializes ApplicationContext twice and Allocates two connection pools


srecinto
Jun 5th, 2008, 03:49 PM
Tech Used List:
JDK 1.6.0.05
apache-tomcat-5.5.26 (Dev Environment)
jboss-4.0.5.GA (Prod Environment)
Hibernate 3.2
Spring 2.0.8
*NOTE* I had to replace any "/" with ":" as well as remove any h-t-t-p or w-w-w references to submit this page to avoid the "you must have 15 posts to submit a url" submission error.

Issue:
Spring initializes ApplicationContext twice and Allocates two connection pools

Description:
In tomcat and JBoss, it appears that the spring ApplicationContext is loading twice, once when the container initially starts up and the second time when the tomcat log says it is deploying the war file.
Now I am not sure if it is creating two ApplicationContexts but, according to the tomcat logs, it appears that they are being initialized twice. The part that leads me to suspect two ApplicationContexts is the connection pool is being allocated twice because I am monitoring the number of initial connections and when the container starts up and spring initializes all the beans, I see the first set of connections initiated.
Shortly after I see the tomcat log
Jun 5, 2008 1:23:39 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive MyApp.war
and the I notice the number of connections increased by the exact amount specified in my connection pool settings.

The problem is that the double loading of beans and the double connection pool allocation is wasting resources. I have seen other posts on duplicate contexts but none of them seem to match this particular issue or their solutions did not resolve this problem.

Here is a summary of the tomcat log on start up:
... Initial tomcat startup

INFO web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO context.support.XmlWebApplicationContext - Refreshing org.springframework.web.context.support.XmlWebAppl icationContext"at"15bdc50: display name [Root WebApplicationContext]; startup date [Thu Jun 05 13:49:09 CDT 2008]; root of context hierarchy



... after this the initial the beans and connection pool gets initialized and the "MyApp" servlet configuration get completed.

DEBUG web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'MyApp' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.C ONTEXT.MyApp]
INFO web.servlet.DispatcherServlet - FrameworkServlet 'MyApp': initialization completed in 166 ms
DEBUG web.servlet.DispatcherServlet - Servlet 'MyApp' configured successfully

... Now this is where it looks like it either reloads or creates a second ApplicationContext in the same container start up

Jun 5, 2008 1:49:45 PM org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
Jun 5, 2008 1:49:45 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive MyApp.war
INFO web.servlet.FormTemplateInitializationContextListe ner - Form template system initialized; template load path = \java\apache-tomcat-5.5.26\webapps\MyApp\WEB-INF
INFO web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO context.support.XmlWebApplicationContext - Refreshing org.springframework.web.context.support.XmlWebAppl icationContext"at"929ba3: display name [Root WebApplicationContext]; startup date [Thu Jun 05 13:49:46 CDT 2008]; root of context hierarchy

... Shortly after this my DB connections increase by the initial pool size and the init of all my spring beans gets recalled all over again leading me to believe a second ApplicationContext is getting initialized.

DEBUG web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'MyApp' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.C ONTEXT.MyApp]
INFO web.servlet.DispatcherServlet - FrameworkServlet 'MyApp': initialization completed in 155 ms
DEBUG web.servlet.DispatcherServlet - Servlet 'MyApp' configured successfully

Here is a summary of the web.xml


<web-app xmlns="java.sun.com:xml:ns:j2ee"

xmlns:xsi="w3.org:2001:XMLSchema-instance"

xsi:schemaLocation="java.sun.com:xml:ns:j2ee java.sun.com:xml:ns:j2ee:web-app_2_4.xsd"

version="2.4">
...

<filter>

<filter-name>Section Path Filter</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterPro xy</filter-class>

<init-param>

<param-name>targetBeanName</param-name>

<param-value>sectionPathFilterBean</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>Section Path Filter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

...

<servlet>

<servlet-name>MyApp</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>:WEB-INF:MyApp-servlet.xml</param-value>
</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>MyApp</servlet-name>

<url-pattern>*.htm</url-pattern>

</servlet-mapping>

... various Servlet Mappings related to MyApp

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

:WEB-INF:applicationContext-hibernate.xml,

:WEB-INF:applicationContext.xml
</param-value>

</context-param>

...

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListe ner</listener-class>

</listener>

</web-app>

applicationContext-hibernate.xml contains dao bean definitions as well as the transactionManager, dataSource, sessionFactory
applicationContext.xml contains business and service beans
MyApp-servlet.xml is an empty context.... just the "beans" tag no content

I have been looking at this for a while and can not figure out why this is occurring so any help would be greatly appreciated

Regards,
Shawn Recinto

srecinto
Jun 10th, 2008, 05:33 PM
I figured it out, the problem was between the keyboard and the chair ;)
It was a configuration in my local tomcat/joboss instances with multiple hosts

Hope this helps anyone who has run into this particular issue

markash
Aug 14th, 2008, 05:54 AM
Hi,

Environment: Jetty 6

I am trying to use the ContextLoaderListener with the DelegatingFilterProxy to setup a Filter that will use beans configured in Spring to allow or disallow a request based upon the state of the application.

I know that I am specifying my context-servlet.xml file twice but I cannot see how to get the ContextLoaderListener to use the DispatcherServler's context.

My web.xml
------------
<web-app xmlns="java.sun.com:xml:ns:j2ee"
xmlns:xsi="w3.org:2001:XMLSchema-instance"
xsi:schemaLocation="java.sun.com:xml:ns:j2ee java.sun.com:xml:ns:j2ee:web-app_2_4.xsd"
version="2.4">

<display-name>Web ActiveMQ</display-name>

<filter>
<filter-name>barrier</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterPro xy</filter-class>
<init-param>
<param-name>beanName</param-name>
<param-value>barrier</param-value>
</init-param>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>barrierFilter</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>barrier</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context-servlet.xml</param-value>
</init-param>
<!--<load-on-startup>1</load-on-startup>-->
</servlet>

<servlet-mapping>
<servlet-name>context</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>context</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<error-page>
<exception-type>com.cibecs.webactivemq.UndermaintenanceException</exception-type>
<location>/undermaintenance.jsp</location>
</error-page>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListe ner</listener-class>
</listener>
</web-app>


My application log
-----------------
Starting jetty 6.1.11 ...
2008-08-14 11:37:48.701::INFO: jetty-6.1.11
2008-08-14 11:37:48.830::INFO: No Transaction manager found - if your webapp requires one, please configure one.
2008-08-14 11:37:48.939:/webactivemq:INFO: Initializing Spring root WebApplicationContext
9 [exec_Build webactivemq_25] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
40 [exec_Build webactivemq_25] INFO org.springframework.web.context.support.XmlWebAppl icationContext - Refreshing org.springframework.web.context.support.XmlWebAppl icationContext@1ee4922: display name [Root WebApplicationContext]; startup date [Thu Aug 14 11:37:48 CAT 2008]; root of context hierarchy
93 [exec_Build webactivemq_25] INFO org.springframework.beans.factory.xml.XmlBeanDefin itionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/context-servlet.xml]
208 [exec_Build webactivemq_25] INFO org.springframework.web.context.support.XmlWebAppl icationContext - Bean factory for application context [org.springframework.web.context.support.XmlWebAppl icationContext@1ee4922]: org.springframework.beans.factory.support.DefaultL istableBeanFactory@95c3a9
324 [exec_Build webactivemq_25] INFO org.springframework.beans.factory.support.DefaultL istableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@95c3a9: defining beans [sendController,org.springframework.context.annotat ion.internalCommonAnnotationProcessor,org.springfr amework.context.annotation.internalAutowiredAnnota tionProcessor,org.springframework.context.annotati on.internalRequiredAnnotationProcessor,viewResolve r,manager,barrierFilter]; root of factory hierarchy
402 [exec_Build webactivemq_25] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 393 ms
2008-08-14 11:37:49.132::INFO: Started SelectChannelConnector@0.0.0.0:8080
Started Jetty Server
Starting scanner at interval of 30 seconds.
2008-08-14 11:37:57.915:/webactivemq:INFO: Initializing Spring FrameworkServlet 'context'
9177 [btpool0-0] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'context': initialization started
9178 [btpool0-0] INFO org.springframework.web.context.support.XmlWebAppl icationContext - Refreshing org.springframework.web.context.support.XmlWebAppl icationContext@4039a2: display name [WebApplicationContext for namespace 'context-servlet']; startup date [Thu Aug 14 11:37:57 CAT 2008]; parent: org.springframework.web.context.support.XmlWebAppl icationContext@1ee4922
9178 [btpool0-0] INFO org.springframework.beans.factory.xml.XmlBeanDefin itionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/context-servlet.xml]
9206 [btpool0-0] INFO org.springframework.web.context.support.XmlWebAppl icationContext - Bean factory for application context [org.springframework.web.context.support.XmlWebAppl icationContext@4039a2]: org.springframework.beans.factory.support.DefaultL istableBeanFactory@d458c7
9209 [btpool0-0] INFO org.springframework.beans.factory.support.DefaultL istableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@d458c7: defining beans [sendController,org.springframework.context.annotat ion.internalCommonAnnotationProcessor,org.springfr amework.context.annotation.internalAutowiredAnnota tionProcessor,org.springframework.context.annotati on.internalRequiredAnnotationProcessor,viewResolve r,manager,barrierFilter]; parent: org.springframework.beans.factory.support.DefaultL istableBeanFactory@95c3a9
9394 [btpool0-0] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'context': initialization completed in 217 ms

Marten Deinum
Aug 14th, 2008, 06:00 AM
I know that I am specifying my context-servlet.xml file twice but I cannot see how to get the ContextLoaderListener to use the DispatcherServler's context.

You cannot and should not...

The ContextLoaderListener is the ROOT application context each (Message)DispatcherServlet has its own context with the ROOT application context as a parent.

Next time please use [ code][/code ] tags when posting code.

markash
Aug 14th, 2008, 07:59 AM
Thank you for the help and I apologize for the code tags.