This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
I have a curious problem... When the Tomcat starts my application, the ContextLoader read my bean definitions in the applicationContext(we split in some files), and when the Spring DispatcherServlet start, read again!
So the problem is, after DispatcherServlet read, I take two hibernate Session Factorys! The session factory created by ContextLoader and the session factory created by DispatcherServlet. That cause a big problem. Somebody know why this happen? And how avoid the second read?
The Hibernate Configuration
The applicationContext.xml (that import some other files, that contain beans definitions)
In some application file that was imported, I have the bean definition that is extended by all the DAO classes:
Thanks [ September 28, 2007: Message edited by: Luciano A. Pozzo ]
Luciano A. Pozzo
Joined: Jun 20, 2005
I found the problem. So I am posting the solution bacause somebody could have the same situation:
My application context was divided in some files for make easy the maintenance. And these files had been grouped in a main file through the tag import of spring.
The problem is that the ContextLoaderListener read and load the bean definitions of this "main file" and after the DispatcherServlet of spring mvc do the same thing again. So what happen?! I have two differents sessionFactory objects because was load twice. Then the OSIV do not work, because always open and close one session. That it is not the one used by the Persistence Manager.
So, what I did to solve. I divided the main file in two. One with the spring mvc definitions (beans that spring mvc need). And other with my business and data access (includes the sessionFactory). So, the Spring DispacherServlet load the definitions that need and the ContextLoaderListener the other one, and everything is loaded once and works.
Pay attention that Spring DispactherServlet read the xml using the name of the servlet (that you defined) more -servlet.xml. Example:
(Your xml should be dispatcher-servlet.xml)
And in your contextConfigLocation, put the other xml. Like this:
Hope that it's clear.
subject: Spring MVC read again applicationContext.xml?