spring ibatis 事务配置

Spring下由三种途径对事物进行管理:编程式事务管理、声明式事务管理和AOP事务管理。其中AOP事务管理又分AOP注解事务管理和AOP XML配置两种,这里记录下述其中的AOP XML配置管理,这也是spring最推荐的方式。

  参照<spring高级程序设计>中的银行转账的例子。

1.Spring的数据源设置

Xml代码   收藏代码
  1. < bean   id = "dataSource"   class = "org.apache.commons.dbcp.BasicDataSource"   destroy-method = "close"   >   
  2.   < property   name = "driverClassName"   value = "com.mysql.jdbc.Driver" />   
  3.   < property   name = "url"   value = "jdbc:mysql://localhost:3306/test" />   
  4.   < property   name = "username"   value = "root" />   
  5.   < property   name = "password"   value = "123456" />   
  6. </ bean >   

 2.Spring对iBATIS的支持

   Spring对ibatis主要提供org.springframework.orm.ibatis.SqlMapClientFactoryBean类来进行支持  

Xml代码   收藏代码
  1. < bean   id = "sqlMapClient"   class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >   
  2.   < property   name = "dataSource"   ref = "dataSource" />   
  3.   < property   name = "configLocation"   value = "/config/sqlMapConfig.xml" />   
  4. </ bean >   

  3.Spring对iBATIS DAO的支持

  Spring提供org.springframework.orm.ibatis.support.SqlMapClientDaoSupport来对 iBATIS DAO进行支持,通过调用该类的getSqlMapClientTemplate()方法来获得对iBATIS的控制访问。

Xml代码   收藏代码
  1. < bean   id = "accountDao"   class = "com.hj.dao.AccountDaoImp" >   
  2.    < property   name = "sqlMapClient"   ref = "sqlMapClient" />   
  3. </ bean >      

 

Xml代码   收藏代码
  1. < bean   id = "bankService"   class = "com.hj.bankOps.DefaultBankService" >   
  2.    < property   name = "accountDao"   ref = "bankAccountDao" />   
  3. </ bean >   

   这里DefaultBankService类主要实现BankService接口(提供服务的方法定义),其内部引用一个BankAccountDao实例来对数据库进行访问。BankAccountDao类主要继承SqlMapClientDaoSupport。

 4.Spring 配置事务  

Xml代码   收藏代码
  1. < bean   id = "transactionManager"   class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >   
  2.      < property   name = "dataSource"   ref = "dataSource" />   
  3.   </ bean >    

  5.AOP XML配置事务管理

    1).配置事务通知

Xml代码   收藏代码
  1. < tx:advice   id = "transactionManagerAdivice"   transaction-manager = "transactionManager" >   
  2.    < tx:attributes >   
  3.       < tx:method   name = "*"    
  4.                 isolation = "READ_COMMITTED"    
  5.                 propagation = "REQUIRED"    
  6.                 rollback-for = "java.lang.RuntionException"   />   
  7.    </ tx:attributes >   
  8. </ tx:advice >   

   2).配置切入点和方面

Xml代码   收藏代码
  1. < aop:config >   
  2.    < aop:pointcut   expression = "execution(* com.hj.bankOps.DefaultBankService.*(..))"   id = "bankServicePc" />   
  3.    < aop:advisor   advice-ref = "transactionManagerAdivice"   pointcut-ref = "bankServicePc" />     
  4.  </ aop:config >   

  上述execution(* com.hj.bankOps.DefaultBankService.*(..))表达式表示切入点为该类中的任何方法。所以当 DefaultBankService类中方法调用时就会进行事务管理,并且当抛出RuntimeException时,自动进行回滚操作。

  6.遇到的问题

   在<Spring高级程序设计>一书上,对AOP XML事务配置时,其通知部分并没有设置具体属性(缺少 rollback-for="java.lang.RuntionException")

Xml代码   收藏代码
  1. < tx:attributes >   
  2.       < tx:method   name = "*"    
  3.                 isolation = "READ_COMMITTED"    
  4.                 propagation = "REQUIRED"    
  5.                />   
  6.    </ tx:attributes >   

 这样在DefaultBankService方法调用中如果有异常抛出,事务并不进行相应回滚操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值