Spring, Hibernate, Tiles, OpenSessionInViewFilter and LazyInitializationException

大憨熊 提交于 2019-12-13 00:56:32

问题


I received LazyInitializationException on jsp when tried to get access to colection which was lazy loaded:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.navigator.category.alias.CategoryAlias.products, no session or session was closed
    org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:348)
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:224)
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)
    javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)
    org.apache.jsp.WEB_002dINF.jsp.admin.categoryalias.all_005fcategory_005faliases_jsp._jspx_meth_c_005fforEach_005f1(all_005fcategory_005faliases_jsp.java:334)
    org.apache.jsp.WEB_002dINF.jsp.admin.categoryalias.all_005fcategory_005faliases_jsp._jspx_meth_c_005fforEach_005f0(all_005fcategory_005faliases_jsp.java:220)
    org.apache.jsp.WEB_002dINF.jsp.admin.categoryalias.all_005fcategory_005faliases_jsp._jspService(all_005fcategory_005faliases_jsp.java:111)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet._serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
    org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
    org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
    org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103)
    org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96)
    org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
    org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
    org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer.write(ChainedDelegateAttributeRenderer.java:76)
    org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:336)
    org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:210)
    org.apache.tiles.template.InsertAttributeModel.end(InsertAttributeModel.java:126)
    org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:311)
    org.apache.jsp.WEB_002dINF.jsp.admin.common.admin_002dlayout_jsp._jspx_meth_tiles_005finsertAttribute_005f2(admin_002dlayout_jsp.java:158)
    org.apache.jsp.WEB_002dINF.jsp.admin.common.admin_002dlayout_jsp._jspService(admin_002dlayout_jsp.java:88)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet._serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241)
    org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)
    org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
    org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:690)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:644)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:627)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:321)
    org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1157)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:927)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:91)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    com.navigator.filter.RedirectAndSearchEngineFilter.doFilterInternal(RedirectAndSearchEngineFilter.java:125)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 

web.xml:

    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
...
    <servlet>
        <servlet-name>navigator</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/navigator-servlet.xml,
        /WEB-INF/security-context.xml
    </param-value>
</context-param>
<servlet-mapping>
    <servlet-name>navigator</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

navigator-servlet.xml:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions" value="/WEB-INF/tiles.xml" />
    <property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory" />
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
    <property name="requestContextAttribute" value="requestContext" />

    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>

Also I have a couple directive at the begin of navigator-servlet.xml (I am not sure that this all is required):

<tx:annotation-driven />
<mvc:annotation-driven />
<context:component-scan base-package="com.navigator" />
<context:annotation-config />
<mvc:resources mapping="/robots.txt" location="/robots.txt" />
<mvc:default-servlet-handler />

I know that LazyInitializationException is common issue but I spent a lot of time and question on stackoverflow is my last hope to fix it.

UPD1: Hibernate version 3.5.6, Spring - 3.1.0.RELEASE


回答1:


It was issue related to "conflict" between DispatcherServlet and contextConfigLocation. This answer was very helpful to me: https://stackoverflow.com/a/12463483/1462024

For resolving my issue DispatcherServlet should be created in next way (init-param section should be added on DispatcherServlet creation):

<servlet>
    <servlet-name>navigator</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>



回答2:


Your configuration looks OK, and from the stack trace one can see that the OpenSessionInViewFilter is running properly. That leaves us to at least two possibilities:

  1. You have manually closed your session (not very likely)
  2. CategoryAlias is detached (With session.evict, or because your instance is .clone()'ed)


来源:https://stackoverflow.com/questions/14327066/spring-hibernate-tiles-opensessioninviewfilter-and-lazyinitializationexceptio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!