文档章节

hibernate clear() 误用 与 LazyInitializationException: could not initialize proxy - no Session

之渊
 之渊
发布于 02/26 17:17
字数 852
阅读 35
收藏 0

最近遇到一个很奇怪的问题。 就是 不知道hibernate 怎么就爆

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
	at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
	at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:92)
	at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:70)
	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:311)
	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
	at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
	at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
	at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
	at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
	at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
	at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
	at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:683)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
	at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
	at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
	at com.moneyP2P.base.spring.service.BaseService.save(BaseService.java:23)
	at com.moneyP2P.base.spring.service.BaseService$$FastClassByCGLIB$$aefff274.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
	at com.rongpd.pay.basedata.service.AllinpayRechargeRecordService$$EnhancerByCGLIB$$f9ebecbe.save(<generated>)
	at com.rongpd.pay.basedata.service.AllinpayTranxManagerService.allinPayRechargeAfterDo(AllinpayTranxManagerService.java:660)
	at com.rongpd.pay.basedata.service.AllinpayTranxManagerService.allinPayRecharge(AllinpayTranxManagerService.java:602)
	at com.rongpd.pay.basedata.service.AllinpayTranxManagerService$$FastClassByCGLIB$$34d49aa4.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
	at com.rongpd.pay.basedata.service.AllinpayTranxManagerService$$EnhancerByCGLIB$$63a19e0b.allinPayRecharge(<generated>)
	at com.moneyP2P.p2p.VSFP.mobile.spring.controller.AllinPayMobileController.allinpayPayment(AllinPayMobileController.java:508)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:230)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

找不到 hibernate 的 session 的错误??? 可是我明明没有 关闭这个 session啊??

看这个异常是 org.hibernate.LazyInitializationException 懒加载?? 什么意思啊? 懒加载 表的其他对象 异常了,差不多了?

那我们可以修改为 急加载?可是 这个方法操作那么多对象,表都改? 其他地方用不是 都是 急加载了,,,性能啊!!!

那到底是 哪里错问题了呢?

在排除的时候,我是看了好久都没有找到。 有一个 细心的同事 找到了,觉得可能是这个错误:

 public void update(Object obj){
		 dao.clear();// 这里引起的错误
    	dao.update(obj);
    }

注释掉了 这个 clear 就 好了。

分析

开始以为是 hibernate 什么 BUG 什么的呢? 让人看得不明白。 这个 clear 就是 session.clear() 将缓存清空。 可是想想,将缓存清空就清空了,那么再次查询一次数据库就好了,能有什么问题?

想想看看 上面的 懒加载对象, 和看看代码保存就知道了

.....
rechargesService.update(rechargeToSuccess); //这个里面  update 加了clear 
			// 记录充值成功后相关流水
			rechargesService.rechargeReturn(rechargeToSuccess);

			// 记录快捷支付的充值流水数据
			AllinpayRechargeRecord arRecord = new AllinpayRechargeRecord();
			arRecord.setAccountNo(userBank.getBankAccount());// 卡号
			arRecord.setBanktype(userBank.getBanktype());// 银行类型  , 清空了缓存导致 这个 userBank 使用 延迟加载 查询 的 Banktype 的时候,由于缓存清空了数据,所以就获取不到懒加载的数据。
// 也就是懒加载错误,
			arRecord.setOrderNum(reqNo);// 充值流水号
			arRecord.setReAccount(tranAmtDecimal);// 实际充值金额以元为单位
			arRecord.setUserbasicsinfo(userBank.getUserbasicsinfo());
.....



也就是说 hibernate的 报错有点不明确,很容易把人搞蒙了,想错了。 所以 clear 清缓存 要慎重啊, hibernate的方法不能乱用啊!!!.

© 著作权归作者所有

共有 人打赏支持
之渊
粉丝 11
博文 520
码字总数 154962
作品 0
佛山
程序员
Hibernate3 错误: could not initialize proxy - no Ses

Hibernate3 错误: could not initialize proxy - no Session 异常: org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.Abstra......

黄平俊
2009/05/12
1K
2
hibernate4 could not initialize proxy - no Session

用Spring3.2+hibernate4.2搭建开发框架的时候,调用load方法的时候,出现错误:org.hibernate.LazyInitializationException: could not initialize proxy - no Session 具体配置如下: web....

干得好差
2013/08/02
8.4K
3
Hibernate+Redis获取缓存对象出错

在hibernate项目中使用jedis缓存时,当一个对象商品(Goods)中包含另外一个对象商品类别(GoodsCategory),序列化实体后缓存到jedis中,然后取出来,GoodsCategory代理对象不能正常初始化,抛出...

zhuDD
2015/11/17
303
1
Hibernate懒加载

面试题: get、load方法区别? get: 及时加载,只要调用get方法立刻向数据库查询 load:默认使用懒加载,当用到数据的时候才向数据库查询。 懒加载:(lazy) 概念:当用到数据的时候才向数据库...

爪哇程序猿
2015/10/22
165
0
session的load加载方式demo

本文主要采用实例展示load加载对象的方式: 1. BankAccount.java package com.lxh.transaction5; import java.io.Serializable; @SuppressWarnings("serial")public class BankAccount imple......

绿林小子
2015/12/11
24
0

没有更多内容

加载失败,请刷新页面

加载更多

sed命令扩展使用操作

打印某行到某行之间的内容 假若文件test.txt的内容是: ertfff**[abcfd]123324444[rty]**fgfgf 怎么能截取 [abcfd]123324444[rty] 这一部分出来呢? 操作命令: 知道开始行和结...

野雪球
26分钟前
1
0
JVM内存笔记

Hotspot JVM 中的 Java 线程与原生操作系统线程有直接的映射关系。当线程本地存储、缓 冲区分配、同步对象、栈、程序计数器等准备好以后,就会创建一个操作系统原生线程。 Java 线程结束,原...

凌渡
32分钟前
1
0
284. Peeking Iterator

Description Tag: Design Difficulties: Medium Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the pee......

52iSilence7
35分钟前
1
0
防止快速重复点击的两种思维

防止重复执行的两种思维 场景 下单时,提交按钮,因为网络卡顿或者手快重复点击,导致重复提交订单; 微博,更新个人状态或发表评论时,快速多次点击[发送]按钮,导致相同的信息发送多次. 解决思路 ...

黄威
今天
0
0
在windows环境下使用Virtualbox虚拟Debian系统来运行Docker

标题绕口。 我之前一直使用 Virtualbox 和 homestead 来运行我的 PHP 开发环境。最近决心开始尝试 DevOps,使得开发、部署容器化,来化解人为操作失误和环境不兼容等问题造成的各种损失。就打...

zgldh
今天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部