Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInViewInterceptor called but session flushed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInViewInterceptor called but session flushed

    Hi everyone,

    I added OpenSessionInViewInterceptor into my Spring beans so that I could do lazy init of variables from within my views.

    I see that the interceptor is correctly called, and a session is opened:

    Code:
    2005-03-11 00&#58;49&#58;59	2005-03-11 00&#58;49&#58;59,506 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <DispatcherServlet with name 'phatcast' received request for &#91;/phatcast/challenge/1&#93;>
    2005-03-11 00&#58;49&#58;59	2005-03-11 00&#58;49&#58;59,506 DEBUG &#91;org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor&#93; - <Opening single Hibernate session in OpenSessionInViewInterceptor>
    2005-03-11 00&#58;49&#58;59	2005-03-11 00&#58;49&#58;59,516 DEBUG &#91;org.springframework.orm.hibernate.SessionFactoryUtils&#93; - <Opening Hibernate session>
    2005-03-11 00&#58;49&#58;59	2005-03-11 00&#58;49&#58;59,776 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <opened session>
    2005-03-11 00&#58;49&#58;59	2005-03-11 00&#58;49&#58;59,786 DEBUG &#91;org.springframework.transaction.support.TransactionSynchronizationManager&#93; - <Bound value &#91;org.springframework.orm.hibernate.SessionHolder@12fcdf5&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@1032bc9&#93; to thread &#91;http-80-Processor24&#93;>
    2005-03-11 00&#58;49&#58;59	2005-03-11 00&#58;49&#58;59,786 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Testing handler adapter &#91;org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@352d87&#93;>
    2005-03-11 00&#58;49&#58;59	2005-03-11 00&#58;49&#58;59,826 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <opened session>
    The main command object is initialized properly, however it has a couple one-to-many relationships that are lazy-init. I refer to these sets within the JSP itself, where I do a forEach loop through them.

    However, I keep getting a LazyInitializationException (full error below). I notice that the reason is that right after it initializes the main command object, and before it hits the JSP, it closes all the sessions. This means that within the JSP, the forEach automatically causes the LazyInitializationException.

    Code:
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,647 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <done materializing entity &#91;org.phatcast.db.Challenge#1&#93;>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,647 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <initializing non-lazy collections>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,687 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <closing session>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,687 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <disconnecting session>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,727 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <transaction completion>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,727 DEBUG &#91;org.springframework.transaction.support.TransactionSynchronizationManager&#93; - <Retrieved value &#91;org.springframework.orm.hibernate.SessionHolder@12fcdf5&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@1032bc9&#93; bound to thread &#91;http-80-Processor24&#93;>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,727 DEBUG &#91;org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor&#93; - <Flushing single Hibernate session in OpenSessionInViewInterceptor>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,817 DEBUG &#91;org.springframework.web.servlet.view.InternalResourceViewResolver&#93; - <Cached view 'challenge-view'>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,817 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Rendering view &#91;org.springframework.web.servlet.view.JstlView&#58; name 'challenge-view'; URL &#91;/WEB-INF/jsp/challenge-view.jsp&#93;&#93; in DispatcherServlet with name 'phatcast'>
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,938 ERROR &#91;net.sf.hibernate.LazyInitializationException&#93; - <Failed to lazily initialize a collection - no session or session was closed>
    2005-03-11 00&#58;50&#58;03	net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed
    So two questions:

    1) How do I keep the session open long enough for the JSP to do whatever it calls it needs, from the web layer, and then close it at the end?

    2) Is this expected behavior that the session closes before the lazy-init? Do I have to force the initialization in the Spring controller?

    Thanks,
    Andrew

    Code:
    2005-03-11 00&#58;50&#58;03	2005-03-11 00&#58;50&#58;00,938 ERROR &#91;net.sf.hibernate.LazyInitializationException&#93; - <Failed to lazily initialize a collection - no session or session was closed>
    2005-03-11 00&#58;50&#58;03	net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed
    2005-03-11 00&#58;50&#58;03	at net.sf.hibernate.collection.PersistentCollection.initialize&#40;PersistentCollection.java&#58;209&#41;
    2005-03-11 00&#58;50&#58;03	at net.sf.hibernate.collection.PersistentCollection.read&#40;PersistentCollection.java&#58;71&#41;
    2005-03-11 00&#58;50&#58;03	at net.sf.hibernate.collection.Set.toString&#40;Set.java&#58;217&#41;
    2005-03-11 00&#58;50&#58;03	at java.lang.String.valueOf&#40;String.java&#58;2131&#41;
    2005-03-11 00&#58;50&#58;03	at java.lang.StringBuffer.append&#40;StringBuffer.java&#58;370&#41;
    2005-03-11 00&#58;50&#58;03	at java.util.AbstractMap.toString&#40;AbstractMap.java&#58;589&#41;
    2005-03-11 00&#58;50&#58;03	at java.lang.String.valueOf&#40;String.java&#58;2131&#41;
    2005-03-11 00&#58;50&#58;03	at java.lang.StringBuffer.append&#40;StringBuffer.java&#58;370&#41;
    2005-03-11 00&#58;50&#58;03	at java.util.AbstractMap.toString&#40;AbstractMap.java&#58;589&#41;
    2005-03-11 00&#58;50&#58;03	at java.lang.String.valueOf&#40;String.java&#58;2131&#41;
    2005-03-11 00&#58;50&#58;04	at java.lang.StringBuffer.append&#40;StringBuffer.java&#58;370&#41;
    2005-03-11 00&#58;50&#58;04	at org.springframework.web.servlet.view.AbstractView.render&#40;AbstractView.java&#58;231&#41;
    2005-03-11 00&#58;50&#58;04	at org.springframework.web.servlet.DispatcherServlet.render&#40;DispatcherServlet.java&#58;839&#41;
    2005-03-11 00&#58;50&#58;04	at org.springframework.web.servlet.DispatcherServlet.doDispatch&#40;DispatcherServlet.java&#58;616&#41;
    2005-03-11 00&#58;50&#58;04	at org.springframework.web.servlet.DispatcherServlet.doService&#40;DispatcherServlet.java&#58;543&#41;
    2005-03-11 00&#58;50&#58;04	at org.springframework.web.servlet.FrameworkServlet.serviceWrapper&#40;FrameworkServlet.java&#58;390&#41;
    2005-03-11 00&#58;50&#58;04	at org.springframework.web.servlet.FrameworkServlet.doGet&#40;FrameworkServlet.java&#58;341&#41;
    2005-03-11 00&#58;50&#58;04	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;689&#41;
    2005-03-11 00&#58;50&#58;04	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;Appl	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;237&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperValve.java&#58;214&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardContextValve.invokeInternal&#40;StandardContextValve.java&#58;198&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextValve.java&#58;152&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.java&#58;137&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.java&#58;117&#41;
    2005-03-11 00&#58;50&#58;04	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;102&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineValve.java&#58;109&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;929&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.coyote.tomcat5.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;160&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java&#58;799&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection&#40;Http11Protocol.java&#58;705&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.tomcat.util.net.TcpWorkerThread.runIt&#40;PoolTcpEndpoint.java&#58;577&#41;
    2005-03-11 00&#58;50&#58;05	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadPool.java&#58;683&#41;
    2005-03-11 00&#58;50&#58;05	at java.lang.Thread.run&#40;Thread.java&#58;534&#41;
    2005-03-11 00&#58;50&#58;05	2005-03-11 00&#58;50&#58;01,018 DEBUG &#91;org.springframework.transaction.support.TransactionSynchronizationManager&#93; - <Removed value &#91;org.springframework.orm.hibernate.SessionHolder@12fcdf5&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@1032bc9&#93; from thread &#91;http-80-Processor24&#93;>
    2005-03-11 00&#58;50&#58;05	2005-03-11 00&#58;50&#58;01,018 DEBUG &#91;org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor&#93; - <Closing single Hibernate session in OpenSessionInViewInterceptor>
    2005-03-11 00&#58;50&#58;05	2005-03-11 00&#58;50&#58;01,018 DEBUG &#91;org.springframework.orm.hibernate.SessionFactoryUtils&#93; - <Closing Hibernate session>
    2005-03-11 00&#58;50&#58;05	2005-03-11 00&#58;50&#58;01,018 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <closing session>
    2005-03-11 00&#58;50&#58;05	2005-03-11 00&#58;50&#58;01,058 ERROR &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Could not complete request>
    2005-03-11 00&#58;50&#58;05	net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed
    2005-03-11 00&#58;50&#58;05	at net.sf.hibernate.collection.PersistentCollection.initialize&#40;PersistentCollection.java&#58;209&#41;
    
    2005-03-11 00&#58;50&#58;05	at net.sf.hibernate.collection.PersistentCollection.read&#40;PersistentCollection.java&#58;71&#41;
    2005-03-11 00&#58;50&#58;05	at net.sf.hibernate.collection.Set.toString&#40;Set.java&#58;217&#41;
    2005-03-11 00&#58;50&#58;05	at java.lang.String.valueOf&#40;String.java&#58;2131&#41;
    2005-03-11 00&#58;50&#58;05	at java.lang.StringBuffer.append&#40;StringBuffer.java&#58;370&#41;
    2005-03-11 00&#58;50&#58;06	at java.util.AbstractMap.toString&#40;AbstractMap.java&#58;589&#41;
    2005-03-11 00&#58;50&#58;06	at java.lang.String.valueOf&#40;String.java&#58;2131&#41;
    2005-03-11 00&#58;50&#58;06	at java.lang.StringBuffer.append&#40;StringBuffer.java&#58;370&#41;
    2005-03-11 00&#58;50&#58;06	at java.util.AbstractMap.toString&#40;AbstractMap.java&#58;589&#41;
    2005-03-11 00&#58;50&#58;06	at java.lang.String.valueOf&#40;String.java&#58;2131&#41;
    2005-03-11 00&#58;50&#58;06	at java.lang.StringBuffer.append&#40;StringBuffer.java&#58;370&#41;
    2005-03-11 00&#58;50&#58;06	at org.springframework.web.servlet.view.AbstractView.render&#40;AbstractView.java&#58;231&#41;
    2005-03-11 00&#58;50&#58;06	at org.springframework.web.servlet.DispatcherServlet.render&#40;DispatcherServlet.java&#58;839&#41;
    2005-03-11 00&#58;50&#58;06	at org.springframework.web.servlet.DispatcherServlet.doDispatch&#40;DispatcherServlet.java&#58;616&#41;
    2005-03-11 00&#58;50&#58;06	at org.springframework.web.servlet.DispatcherServlet.doService&#40;DispatcherServlet.java&#58;543&#41;
    2005-03-11 00&#58;50&#58;06	at org.springframework.web.servlet.FrameworkServlet.serviceWrapper&#40;FrameworkServlet.java&#58;390&#41;
    2005-03-11 00&#58;50&#58;06	at org.springframework.web.servlet.FrameworkServlet.serviceWrapper&#40;FrameworkServlet.java&#58;390&#41;
    2005-03-11 00&#58;50&#58;06	at org.springframework.web.servlet.FrameworkServlet.doGet&#40;FrameworkServlet.java&#58;341&#41;
    2005-03-11 00&#58;50&#58;06	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;689&#41;
    2005-03-11 00&#58;50&#58;06	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
    2005-03-11 00&#58;50&#58;06	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;237&#41;
    2005-03-11 00&#58;50&#58;06	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
    2005-03-11 00&#58;50&#58;06	at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperValve.java&#58;214&#41;
    2005-03-11 00&#58;50&#58;06	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
    2005-03-11 00&#58;50&#58;06	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
    2005-03-11 00&#58;50&#58;06	at org.apache.catalina.core.StandardContextValve.invokeInternal&#40;StandardContextValve.java&#58;198&#41;
    2005-03-11 00&#58;50&#58;06	at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextValve.java&#58;152&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.java&#58;137&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.java&#58;117&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;102&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineValve.java&#58;109&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;929&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.coyote.tomcat5.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;160&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java&#58;799&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection&#40;Http11Protocol.java&#58;705&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.tomcat.util.net.TcpWorkerThread.runIt&#40;PoolTcpEndpoint.java&#58;577&#41;
    2005-03-11 00&#58;50&#58;07	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadPool.java&#58;683&#41;
    2005-03-11 00&#58;50&#58;07	at java.lang.Thread.run&#40;Thread.java&#58;534&#41;
    2005-03-11 00&#58;50&#58;07	2005-03-11 00&#58;50&#58;01,138 DEBUG &#91;org.springframework.web.context.support.XmlWebApplicationContext&#93; - <Publishing event in context &#91;WebApplicationContext for namespace 'phatcast-servlet'&#93;&#58; RequestHandledEvent&#58; url=&#91;/phatcast/challenge/1&#93;; time=&#91;1552ms&#93;; client=&#91;127.0.0.1&#93;; method=&#91;GET&#93;; servlet=&#91;phatcast&#93;; session=&#91;25CA37765C84B40556B6B2C54255772D&#93;; user=&#91;null&#93;; status=&#91;failed&#58; net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed&#93;>
    2005-03-11 00&#58;50&#58;07	2005-03-11 00&#58;50&#58;04,703 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <running Session.finalize&#40;&#41;>
    2005-03-11 00&#58;50&#58;07	2005-03-11 00&#58;50&#58;04,703 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <running Session.finalize&#40;&#41;>

  • #2
    I have used on the web tier the OpenSessionInViewFilter not the interceptor because the filter acts around the http request which is more natural for a web application - that is the filter will close the connection after the page is rendered; however, I am not using Spring MVC (which probably can use the interceptor very well).

    Comment

    Working...
    X