问题:
could not initialize proxy - the owning Session was closed
参考如下的帖子:http://lz726.iteye.com/blog/116616
问题的描述:
在使用quartz定时扫描处理客户上行短信的方法中,调用自己定义的一个Service层中的一个load一个实体对象如Member
member = load(1)返回到该方法使用,
由于是采用load方法,所以返回的是真正实体对象的一个代理,而不是真正的实体对象。返回后,
由于调用自己定义的一个Service层后事务所对应的SessionFactory就已经关闭了,
然后再重新回到自己定义的方法中的时候,Service中SessionFactory已经关闭当然其对应的session也已经关闭,此时我们在去调用
我们load过来的对象的某个方法的时候如member.getName(),此时SessionFactory已经关闭,所以就会报
could not initialize proxy - the owning Session was closed 这个异常,
解决办法是什么呢?
在我们的applicationContext-quartz.xml文件中动态注入一个SessionFactory即可。参考如下的代码片段:
<beans>
<!-- The Hibernate interceptor, which takes care of opening and closing hibernate session around method calls. -->
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="quartzSchedulerFactory" lazy
.................
</beans>
同时定义一个代理
<bean id="pointPaymentCouponIncomingSmsHandler" class="com.milesup.scheduler.handler.PointPaymentCouponIncomingSmsHandler">
<property name="telecomService">
<ref bean="telecomService"/>
</property>
。。。。。。。。。。。。。。。。。
</bean>
<!-- A proxy with the hibernate interceptor wired in so it can access the persistent context -->
<bean id="pointPaymentCouponIncomingSmsHandlerProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="pointPaymentCouponIncomingSmsHandler"/>
</property>
<property name="interceptorNames">
<value>hibernateInterceptor</value>
</property>
</bean>
以上就是采用AOP技术,在运行时动态注入一个hibernateInterceptor即注入一个SessionFactory的方式解决这个问题。
具体参考附件
分享到:
相关推荐
解决org.hibernate.LazyInitializationException: could not initialize proxy - no Session...
NULL 博文链接:https://smartgwt.iteye.com/blog/1252268
并且对Dao和Service进行了封装,内含.jar包,并且解决了一对多双向关联的could not initialize proxy - no Session错误,同时解决了……invoked on net.sf.json.JSONException: There is a cycle in the hierarchy!...
主要介绍了Plsql Developer连接Oracle时出现Could not initialize oci.dll解决方案,需要的朋友可以参考下
解决:oracle_plsql连服务端时 Initialization error could initialize错误处理方法
监听COM口启动程序报错 Could not initialize class gnu.io.RXTXCommDriver异常 是因为需要在%JAVA_HOME%/jre/bin目录中添加rxtxParallel.dll、rxtxSerial.dll文件
内涵jacob_1.14.3-x64.dll;jacob_1.14.3-64.jdk(maven地址);解决方法:Could not initialize class com.jacob.com.ComThread
初始化数组,对于在VC++编译环境下,该代码是非常有帮助的,可以让读者快速学习数组的应用。
使用javacpp技术,在32位linux系统上实现视频截图功能,注意so文件放在liberary_path里面
Could not initialize "D:\app\Happy\product\11.2.0\dbhome_1\bin\oci.dll" Make sure you have the 32 bits Oracle Client installed. OracleHomeKey: OracleHomeDir: D:\app\Happy\product\11.2.0\dbhome_1 ...
java.lang.NoClassDefFoundError: Could not initialize class org.apache.derby.jdbc.AutoloadedDriver40 导致的原因: 在azkaban的server和executor中缺少一个叫derby.jar的包
7.注册如果发送邮件激活的方式出错(返回页面错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session) 8.禁止用户后不允许登录、发帖、回帖等 9.后台会员搜索中文名搜索乱码 ...
instantclient-basic-windows.x64-11.2.0.4.0
IDA6.4中支持pin的动态二进制插桩功能,这是其中的描述文档
connector's end point moving, the link is breaks if the move was not on one of the flex-controls connection points. Contain False as default. - ADD Added ControlDocRect parameter in event ...
Win7 x64 PL/SQL 连接 Oralce 提示 Could not initialize "%ORACLE_HOME%\bin\oci.dll" 缺少oci文件
In addition to the default Bochs method using the CTRL key and the middle mouse button there are now the choices: - CTRL+F10 (like DOSBox) - CTRL+ALT (like QEMU) - F12 (replaces win32 'legacyF12'...
unable to initialize windows sockets interface error code 0 WINSOCK坏了
JSONArray.fromObject(map)报错:Could not initialize class net.sf.json.JsonConfig。ireport 需要高于2.1版本的包。于是就找了这些包。最后2.2.2适合