hibernate和spring的事务处理

本文介绍如何将Hibernate与Spring集成,并使用Spring的声明式事务管理。主要内容包括在applicationContext.xml中配置事务管理器、指定哪些类和方法使用事务,以及设置事务的传播特性。

将hibernate和spring集成,使用spring框架的声明式事务。
       使用spring的声明式事务,不再需要自动创建sessionFactory和Session,不再需要手动控制事务的开启和关闭。

       使用spring声明式事务的几个步骤:

       1. applicationContext.xml中进行配置

[html]  view plain  copy
 
  1. <!-- 配置事务管理器 -->      
  2. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">          
  3.      <property name="sessionFactory">              
  4.           <ref bean="sessionFactory"/>  <!-- transactionManager的SetSessionFactory()方法的参数为SessionFactory -->                     </property>      
  5. </bean>          
  6.   
  7. <!-- 那些类那些方法使用事务 -->      
  8. <aop:config>          
  9.     <aop:pointcut id="allManagerMethod" expression="execution(* com.bjpowernode.usermgr.manager.*.*(..))"/>          
  10.     <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>      
  11. </aop:config>           
  12.      
  13.   
  14. <!-- 事务的传播特性 -->        
  15. <tx:advice id="txAdvice" transaction-manager="transactionManager">          
  16.     <tx:attributes>              
  17.         <tx:method name="add*" propagation="REQUIRED"/>              
  18.         <tx:method name="del*" propagation="REQUIRED"/>              
  19.         <tx:method name="modify*" propagation="REQUIRED"/>             
  20.         <tx:method name="*" propagation="REQUIRED" read-only="true"/>          
  21.     </tx:attributes>      
  22. </tx:advice>  
[html]  view plain  copy
 
  1.  2.UserManagerImpl类继承HibernateDaoSupport  
[java]  view plain  copy
 
    1. public class UserManagerImpl extends HibernateDaoSupport{     
    2.      public void addUser(User user)       
    3.      throws Exception {          
    4.      this.getHibernateTemplate().save(user);          
    5.      log.setType("操作日志");          
    6.      log.setTime(new Date());          
    7.      log.setDetail("XXX");                   
    8.      logManager.addLog(log);                   
    9.      throw new Exception();      
    10. }  

转载于:https://www.cnblogs.com/susuhyc/p/6227604.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值