2011-09-23 52 views
3

如何防止在多次请求页面时引发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) 

这使我们能够继续使用我们的拦截器,并确保我们在每个请求中获得了预先加载的部分的新副本。

回答

0

这可能是因为你在会话中保存了一些hibernate对象,这些对象可能有未初始化的代理。

每次按Ctrl + R时,都会打开新的请求,并且在当前请求期间不可能从先前请求初始化代理对象,并且结果LazyInitializationException异常引发。

如果这不是你的情况,那么试着在这个方向挖掘它。

+0

哪个会话? Http会话或Hibernate会话?我将如何验证? – Scott

+0

Http会话,不知何故,您的单元化代理被转发到下一个请求。我只是建议你看看你的应用程序逻辑或者只是调试它。 –

+0

我仔细检查了一下。也许有些东西坚持重定向。 – Scott

0

您确定实际调用了拦截器类吗?获取源代码,并对其进行调试以查看执行是否真正达到它。请确保你实际上已经宣布拦截在您的Spring MVC配置的拦截器:

<bean id="urlMapping"class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
<property name="interceptors"> 
<list> 
<ref bean="openSessionInViewInterceptor"/> 
</list> 
</property> 

<property name="mappings"> 

</bean> 

或者,你可以用简单的OpenSessionInViewFilter,只需要配置一个Servlet过滤器进入其截取/ *(或者一个通用的enoug URL来包含处理hibernate实体的控制器的所有url)。

+0

我们收到与OSIVF同样的问题也是如此。 – Scott

+0

相信我,我感觉到你。这是使用Hibernate的“祝福”之一,它是如此糟糕,被包裹在另一个像Spring这样的重量级框架中,你就会迷失在事情中。我选择避免LAzyInitialisation ...是从一开始就使用“获取连接”来为当前视图获取所有需要的数据。我的意思是,为什么你需要让一个足够的Hibernate发出一堆选择? –

0

写一个类,如: -

public class CustomHibernateSessionViewFilter extends OpenSessionInViewFilter { 

protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { 
    Session session = super.getSession(sessionFactory); 
    session.setFlushMode(FlushMode.COMMIT); 
    return session; 
} 

protected void closeSession(Session session, SessionFactory factory) { 
    session.flush(); 
    super.closeSession(session, factory); 
} 

}

声明它在这样的web.xml: -

<filter> 
    <filter-name>OSIVF Filter</filter-name> 
    <filter-class>your.path.to.CustomHibernateSessionViewFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>OSIVF Filter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping>