如何防止在多次请求页面时引发LazyInitializationException?如果我只是在我的web应用程序的一个页面上按住Ctrl-R,我始终会在我的日志文件中收到此消息。来自刷新的延迟初始化错误
我在servlet.xml文件配置了以下拦截器:
<mvc:interceptors>
<bean
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor" />
</mvc:interceptors>
然而,我不断收到以下错误:
2011-09-23 15:14:28,854 [http-8080-23] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/web-app].[springmvc] - Servlet.service() for servlet springmvc threw exception
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:366)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
注:在拦截记录打开我清楚地看到,它被调用并打开/关闭交易:
2011-09-23 15:36:53,229 [http-8080-5] DEBUG org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Opening single Hibernate Session in OpenSessionInViewInterceptor
2011-09-23 15:36:53,229 [http-8080-5] WARN eqip.core.springmvc.extensions.interceptors.AbstractAgencyDataInterceptor IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - Pre handle: http://134.167.141.34:8080/web-app/main.xhtml Status: 200
2 011-09-23 15:36:53,511 [http-8080-5] WARN org.hibernate.engine.StatefulPersistenceContext.ProxyWarnLog IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - 将代理缩小到class core.model .entities.Subclass - 此操作中断== 2011-09-23 15:36:53,511 [http-8080-5] WARN org.hibernate.engine.StatefulPersistenceContext.ProxyWarnLog IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - 缩小代理类到core.model.entities.Subclass - 此操作中断== 2011-09-23 15:36:53,916 [http-8080-5] DEBUG org.springframework.orm.hibernate3.support。 OpenSessionInViewInterceptor IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - 在OpenSessionInViewInterceptor中刷新单个Hibernate会话2011-09-23 15:36:53,916 [http-8080-5] DEBUG org.springframework.web.servlet。 DispatcherServlet IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - 渲染视图[eqip.core.springmvc.extensions.velocity.Velocit yToolsLayoutView:name'pages/myEqip'; DispatcherServlet中的URL [pages/main.xhtml]],名称为'springmvc' 2011-09-23 15:36:54,213 [http-8080-5] DEBUG eqip.core.springmvc.extensions.velocity.VelocityToolsLayoutView IP134.167.141。 34 CV#ef955014-cc9d-42fc P#75004 - 渲染屏幕内容模板[pages/main.xhtml] 2011-09-23 15:36:54,384 [http-8080-5] DEBUG org.springframework.orm.hibernate3。 support.OpenSessionInViewInterceptor IP134.167.141.34 CV#ef955014-cc9d-42fc P#75004 - 中的OpenSessionInViewInterceptor
关闭单个的Hibernate Session使用Spring 3.0.5,3.6.5休眠,速度1.7
最终修复 :将以下内容添加到我们的控制器声明中:
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
这使我们能够继续使用我们的拦截器,并确保我们在每个请求中获得了预先加载的部分的新副本。
哪个会话? Http会话或Hibernate会话?我将如何验证? – Scott
Http会话,不知何故,您的单元化代理被转发到下一个请求。我只是建议你看看你的应用程序逻辑或者只是调试它。 –
我仔细检查了一下。也许有些东西坚持重定向。 – Scott