Spring framework3.2整合hibernate4.1報錯:No Session found for current thread


<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
<property name="dataSource" ref="dataSource" /> 
</bean> 
<tx:advice id="txAdvice" transaction-manager="txManager"> 
<tx:attributes> 
<tx:method name="save*" propagation="REQUIRED"/> 
<tx:method name="add*" propagation="REQUIRED"/> 
<tx:method name="create*" propagation="REQUIRED"/> 
<tx:method name="insert*" propagation="REQUIRED"/> 
<tx:method name="*" read-only="true" /> 
</tx:attributes> 
</tx:advice> 

<aop:config proxy-target-class="true"> 
<aop:advisor advice-ref="txAdvice" pointcut="execution(* net.noday..service..*.*(..))" /> 
</aop:config>

getCurrentSession需要做如下配置:

<propertyname="current_session_context_class" >thread</property>

1. 前者打開一個新的,后者當前有session的話,則是使用當前的session,沒有的話則創建一個新的;

2. 如果使用前者獲得一個session的話,需要手動關閉session,使用后者,當事務提交,session會自動關閉,如果再關閉session則會報如下異常:Session has already closed;

3. sf.getCurrentSession方法需要在hibernate.cfg.xml做如下配置:

    <propertynamepropertyname="current_session_context_class" >thread</property>  

沒有配置的話會報如下異常:org.hibernate.HibernateException:No CurrentSessionContext configured!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM