Spring事務管理
Spring支持兩種方式的事務管理:
- 編程式事務管理: 通過Transaction Template手動管理事務,實際應用中很少使用,
- 使用XML配置聲明式事務: 推薦使用(代碼侵入性最小),實際是通過AOP實現
- 基於 TransactionInterceptor 的聲明式事務:Spring 聲明式事務的基礎,通常也不建議使用這種方式,但是與前面一樣,瞭解這種方式對理解 Spring 聲明式事務有很大作用。
- 基於 TransactionProxyFactoryBean 的聲明式事務:第一種方式的改進版本,簡化的配置文件的書寫,這是 Spring 早期推薦的聲明式事務管理方式,但是在 Spring 2.0 中已經不推薦了。
- 基於< tx> 和< aop>命名空間的聲明式事務管理:目前推薦的方式,其最大特點是與 Spring AOP 結合緊密,可以充分利用切點表達式的強大支持,使得管理事務更加靈活。
- 基於 @Transactional 的全註解方式:將聲明式事務管理簡化到了極致。開發人員只需在配置文件中加上一行啟用相關後處理 Bean 的配置,然後在需要實施事務管理的方法或者類上使用 @Transactional 指定事務規則即可實現事務管理,而且功能也不必其他方式遜色。
再來說一下這個案例的思想吧,我們在兩次轉賬之間添加一個錯誤語句(對應銀行斷電等意外情況),如果這個時候兩次轉賬不能成功,則說明事務配置正確,否則,事務配置不正確。
你需要完成的任務:
- 使用編程式事務管理完成轉賬業務
- 使用基於AspectJ的聲明式事務管理完成轉賬業務
- 使用基於 @Transactional 的全註解方式事務管理完成轉賬業務
備註:
下面的代碼是在很久之前,我剛學Sping還沒有接觸Maven的時候寫的,所以我使用的原始添加jar的方式,使用Maven的小夥伴可以自行添加Maven依賴,沒有使用Maven的小夥伴直接使用我下面提供的jar包即可。
項目結構:
開發工具:
Myeclipse2017
SQL:
create table `account` (`username` varchar (99),`salary` int (11)); insert into `account` (`username`, `salary`) values('小王','3000');insert into `account` (`username`, `salary`) values('小馬','3000');
(1)編程式事務管理
注意:通過添加/刪除accountMoney() 方法中int i = 10 / 0這個語句便可驗證事務管理是否配置正確。
OrdersDao.java(Dao層)
package cn.itcast.dao;import org.springframework.jdbc.core.JdbcTemplate;public class OrdersDao {// 注入jdbcTemplate模板對象private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}// 對數據操作的方法不包含業務操作/** * 小王少錢的方法 */public void reduceMoney() {String sql = "update account set salary=salary-? where username=?";jdbcTemplate.update(sql, 1000, "小王");}/** * 小馬多錢的方法 */public void addMoney() {String sql = "update account set salary=salary+? where username=?";jdbcTemplate.update(sql, 1000, "小馬");}}
OrdersService.java(業務邏輯層)
package cn.itcast.service;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.TransactionCallback;import org.springframework.transaction.support.TransactionTemplate;import cn.itcast.dao.OrdersDao;public class OrdersService {// 注入Dao層對象private OrdersDao ordersDao;public void setOrdersDao(OrdersDao ordersDao) {this.ordersDao = ordersDao;}// 注入TransactionTemplate對象private TransactionTemplate transactionTemplate;public void setTransactionTemplate(TransactionTemplate transactionTemplate) {this.transactionTemplate = transactionTemplate;}// 調用dao的方法// 業務邏輯,寫轉賬業務public void accountMoney() {transactionTemplate.execute(new TransactionCallback
TestService.java(測試方法)
package cn.itcast.service;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestService {@Testpublic void testAdd() {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");OrdersService userService = (OrdersService) context.getBean("ordersService");userService.accountMoney();}}
配置文件:
OrdersService.java(業務邏輯層)
package cn.itcast.service;import cn.itcast.dao.OrdersDao;public class OrdersService {private OrdersDao ordersDao;public void setOrdersDao(OrdersDao ordersDao) {this.ordersDao = ordersDao;}// 調用dao的方法// 業務邏輯,寫轉賬業務public void accountMoney() {// 小馬多1000ordersDao.addMoney();// 加入出現異常如下面int i=10/0(銀行中可能為突然停電等。。。);結果:小馬賬戶多了1000而小王賬戶沒有少錢// 解決辦法是出現異常後進行事務回滾int i = 10 / 0;// 事務管理配置後異常已經解決// 小王 少1000ordersDao.reduceMoney();}}
配置文件:
(3)基於註解的方式
OrdersService.java(業務邏輯層)
package cn.itcast.service;import org.springframework.transaction.annotation.Isolation;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import cn.itcast.dao.OrdersDao;@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false, timeout = -1)public class OrdersService {private OrdersDao ordersDao;public void setOrdersDao(OrdersDao ordersDao) {this.ordersDao = ordersDao;}// 調用dao的方法// 業務邏輯,寫轉賬業務public void accountMoney() {// 小馬多1000ordersDao.addMoney();// 加入出現異常如下面int i=10/0(銀行中可能為突然停電等。。。);結果:小馬賬戶多了1000而小王賬戶沒有少錢// 解決辦法是出現異常後進行事務回滾// int i = 10 / 0;// 事務管理配置後異常已經解決// 小王 少1000ordersDao.reduceMoney();}}
配置文件:
閱讀更多 java架構之路 的文章