<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!