Spring下由三种途径对事物进行管理:编程式事务管理、声明式事务管理和AOP事务管理。其中AOP事务管理又分AOP注解事务管理和AOP XML配置两种,这里记录下述其中的AOP XML配置管理,这也是spring最推荐的方式。
参照<spring高级程序设计>中的银行转账的例子。
1.Spring的数据源设置
- < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" >
- < property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
- < property name = "url" value = "jdbc:mysql://localhost:3306/test" />
- < property name = "username" value = "root" />
- < property name = "password" value = "123456" />
- </ bean >
2.Spring对iBATIS的支持
Spring对ibatis主要提供org.springframework.orm.ibatis.SqlMapClientFactoryBean类来进行支持
- < bean id = "sqlMapClient" class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
- < property name = "dataSource" ref = "dataSource" />
- < property name = "configLocation" value = "/config/sqlMapConfig.xml" />
- </ bean >
3.Spring对iBATIS DAO的支持
Spring提供org.springframework.orm.ibatis.support.SqlMapClientDaoSupport来对 iBATIS DAO进行支持,通过调用该类的getSqlMapClientTemplate()方法来获得对iBATIS的控制访问。
- < bean id = "accountDao" class = "com.hj.dao.AccountDaoImp" >
- < property name = "sqlMapClient" ref = "sqlMapClient" />
- </ bean >
- < bean id = "bankService" class = "com.hj.bankOps.DefaultBankService" >
- < property name = "accountDao" ref = "bankAccountDao" />
- </ bean >
这里DefaultBankService类主要实现BankService接口(提供服务的方法定义),其内部引用一个BankAccountDao实例来对数据库进行访问。BankAccountDao类主要继承SqlMapClientDaoSupport。
4.Spring 配置事务
- < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
- < property name = "dataSource" ref = "dataSource" />
- </ bean >
5.AOP XML配置事务管理
1).配置事务通知
- < tx:advice id = "transactionManagerAdivice" transaction-manager = "transactionManager" >
- < tx:attributes >
- < tx:method name = "*"
- isolation = "READ_COMMITTED"
- propagation = "REQUIRED"
- rollback-for = "java.lang.RuntionException" />
- </ tx:attributes >
- </ tx:advice >
2).配置切入点和方面
- < aop:config >
- < aop:pointcut expression = "execution(* com.hj.bankOps.DefaultBankService.*(..))" id = "bankServicePc" />
- < aop:advisor advice-ref = "transactionManagerAdivice" pointcut-ref = "bankServicePc" />
- </ aop:config >
上述execution(* com.hj.bankOps.DefaultBankService.*(..))表达式表示切入点为该类中的任何方法。所以当 DefaultBankService类中方法调用时就会进行事务管理,并且当抛出RuntimeException时,自动进行回滚操作。
6.遇到的问题
在<Spring高级程序设计>一书上,对AOP XML事务配置时,其通知部分并没有设置具体属性(缺少 rollback-for="java.lang.RuntionException")
- < tx:attributes >
- < tx:method name = "*"
- isolation = "READ_COMMITTED"
- propagation = "REQUIRED"
- />
- </ tx:attributes >
这样在DefaultBankService方法调用中如果有异常抛出,事务并不进行相应回滚操作。