could not initialize proxy - the owning Session was closed

 

問題:

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的方式解決這個問題。

 

具體參考附件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章