在每一个常见企业应用系统中,我们经常都会把登陆的用户对象User放到session中,而且我们经常都会在action中写一个便捷方法getCurrentUser()方法,有一些经验不足的使用者就会把user传递到service层的接口,有的甚至把该对象传递到DAO层
在使用User对象的引用变量是就会抛出如下的错误,其实这就是跨session访问的一个典型错误。这里有两个解决方式:
1.每次调用service接口时,都只传递userId,在service层内用DAO接口去获得一个新的持久化对象(一个规范化的系统,基本上都是在service层启动新的事务,然后启动一个hibernate session)
2.若觉得上面的方法比较麻烦,则可以在hibernate中把可能访问到的引用变量的配置为lazy="false",这种方法缺点也是很明显的:第一点,到底有多少变量可以设置为lazy="false",我们不得而知。若太多,则丢了性能调优的好处,相当于断了用lazy来进行性能调优的后路;若太少,若再有开发人员访问别的引用变量,为了让自己的代码可以运行起来,那他就会修改lazy的设置(尤其是老手,都会经常用这种不负责任的方法),这就很容易把hibernate配置搞的很混乱;第二点,不清楚这个引用变量是否还有引用变量,若有,是否也要设置,这就搞出了一个无穷无尽的问题。
个人建议:坚决杜绝第二种方法,不能因为这种方法暂时可能会省事(针对单个开发人员,但是对于整个team,没省多少事),但是后患无穷;第一种方法虽然暂时可能会麻烦一点,其实没麻烦多少,但是没有任何遗留问题;
09:54:34,515 ERROR [LazyInitializationException] - <could not initialize proxy - the owning Session was closed>
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at com.company.project.domain.system.ShareMode$$EnhancerByCGLIB$$71d9b475.equals(<generated>)
at com.company.project.module.customerserving.dao.CustomerServingDAOImpl.listUnhandledAndDenied(CustomerServingDAOImpl.java:485)
at com.company.project.module.customerserving.service.CustomerServingServiceImpl.listUnhandledAndDenied(CustomerServingServiceImpl.java:354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy17.listUnhandledAndDenied(Unknown Source)
at com.company.project.module.customerserving.action.CustomerServingAction.listUnhandled(CustomerServingAction.java:506)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.company.project.web.filter.AbstractFilter.doFilter(AbstractFilter.java:56)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
09:54:34,531 ERROR [[action]] - <Servlet.service() for servlet action threw exception>
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at com.company.project.domain.system.ShareMode$$EnhancerByCGLIB$$71d9b475.equals(<generated>)
at com.company.project.module.customerserving.dao.CustomerServingDAOImpl.listUnhandledAndDenied(CustomerServingDAOImpl.java:485)
at com.company.project.module.customerserving.service.CustomerServingServiceImpl.listUnhandledAndDenied(CustomerServingServiceImpl.java:354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy17.listUnhandledAndDenied(Unknown Source)
at com.company.project.module.customerserving.action.CustomerServingAction.listUnhandled(CustomerServingAction.java:506)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.company.project.web.filter.AbstractFilter.doFilter(AbstractFilter.java:56)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
分享到:
相关推荐
标题中的"Dwr+Hibernate的Lazy问题"涉及到两个主要技术:Direct Web Remoting (DWR) 和 Hibernate。DWR 是一种让 JavaScript 在浏览器中直接调用服务器端 Java 方法的技术,而 Hibernate 是一个流行的Java持久化框架...
若设为`true`,可以简化HQL语句,但可能引起命名冲突问题;设为`false`时,则需完整指定类的包名。 - **package**: 设定映射文件中类的默认包名。当`auto-import`为`true`时,此属性尤其重要,因为它帮助Hibernate...
它可能导致难以预料的延迟,甚至在某些情况下引起性能问题。因此,懒加载并不总是最优选择,需要根据实际应用的需求和场景来决定是否启用懒加载以及如何处理懒加载引发的问题。 在Hibernate中,理解代理类的工作...
- **Criteria API**: 提供了一种更面向对象的方式来执行查询,避免了字符串拼接引起的SQL注入问题。 - **Query and Native SQL**: Hibernate也支持直接使用SQL进行查询,并可以通过Query接口进行结果集的转换。 5...
- 当关联表数据量较大时,可能会引起性能问题。 ##### 2. Lazy(懒加载) - **定义**:懒加载是指当查询一个实体时,并不会立即加载其关联的实体,而是等到真正使用这些关联实体时才进行加载。 - **优点**: - ...
这是Hibernate3引入的新特性,对于包含重量级大数据的表字段,这种抽取方式提高了对大字段操作的灵活性,否则加载Tfile对象的结果集时如果总是返回fileContent,这种批量的数据抽取将可以引起数据库的"洪泛效应"。...
例如,级联删除允许在删除人员时自动删除其所属的组,但需小心外键约束引起的错误。 以上知识点涵盖了Hibernate中对象生命周期管理、数据操作方法差异、执行控制以及关联关系处理的核心概念,对于深入理解Hibernate...
### 使用Spring引起的错误:Write ...通过以上解决方案和配置示例,可以有效地解决在使用Spring框架和Hibernate时出现的“Write operations are not allowed in read-only mode (FlushMode.NEVER)”这一问题。
- 在配置文件中添加XML配置可能会引起可移植性问题。 接着,关于SessionFactory,它是Hibernate中的一个概念,代表单一数据存储的抽象。SessionFactory是线程安全的,允许多个线程并发访问,并请求会话以及不可变的...
3. **版本兼容性问题**:描述中提到的“版本不同导致的问题”是指在不同的项目或环境中使用不一致的NHibernate版本可能会引起冲突或异常。每个版本可能对API有细微改动,或者修复了某些bug,因此,确保所有相关组件...
- **配置**: 在Hibernate的映射文件中,可以通过设置`lazy="true"`来启用延迟加载。 **3. Ajax的应用与缺点** - **优点**: - 改善用户体验,无需完全重新加载整个页面。 - 提高Web应用的响应速度。 - 可以实现...
### Java面试题详解 #### 1.... Hibernate lazy机制是如何体现的? - **懒加载**:默认情况下,Hibernate会在真正需要时才加载关联对象的数据,这种机制可以提高查询效率,减少不必要的数据库交互。