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
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