05.22 Spring編程式和聲明式事務實例講解

Spring事務管理

Spring支持兩種方式的事務管理:

  • 編程式事務管理: 通過Transaction Template手動管理事務,實際應用中很少使用,
  • 使用XML配置聲明式事務: 推薦使用(代碼侵入性最小),實際是通過AOP實現
  1. 基於 TransactionInterceptor 的聲明式事務:Spring 聲明式事務的基礎,通常也不建議使用這種方式,但是與前面一樣,瞭解這種方式對理解 Spring 聲明式事務有很大作用。
  2. 基於 TransactionProxyFactoryBean 的聲明式事務:第一種方式的改進版本,簡化的配置文件的書寫,這是 Spring 早期推薦的聲明式事務管理方式,但是在 Spring 2.0 中已經不推薦了。
  3. 基於< tx> 和< aop>命名空間的聲明式事務管理:目前推薦的方式,其最大特點是與 Spring AOP 結合緊密,可以充分利用切點表達式的強大支持,使得管理事務更加靈活。
  4. 基於 @Transactional 的全註解方式:將聲明式事務管理簡化到了極致。開發人員只需在配置文件中加上一行啟用相關後處理 Bean 的配置,然後在需要實施事務管理的方法或者類上使用 @Transactional 指定事務規則即可實現事務管理,而且功能也不必其他方式遜色。

我們今天要將的是使用編程式以及基於AspectJ的聲明式和基於註解的事務方式,實現爛大街的轉賬業務。

再來說一下這個案例的思想吧,我們在兩次轉賬之間添加一個錯誤語句(對應銀行斷電等意外情況),如果這個時候兩次轉賬不能成功,則說明事務配置正確,否則,事務配置不正確。

你需要完成的任務:

  • 使用編程式事務管理完成轉賬業務
  • 使用基於AspectJ的聲明式事務管理完成轉賬業務
  • 使用基於 @Transactional 的全註解方式事務管理完成轉賬業務

備註:

下面的代碼是在很久之前,我剛學Sping還沒有接觸Maven的時候寫的,所以我使用的原始添加jar的方式,使用Maven的小夥伴可以自行添加Maven依賴,沒有使用Maven的小夥伴直接使用我下面提供的jar包即可。

項目結構:

Spring編程式和聲明式事務實例講解

開發工具:

Myeclipse2017

SQL:

create table `account` (
\t`username` varchar (99),
\t`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 {
\t// 注入jdbcTemplate模板對象
\tprivate JdbcTemplate jdbcTemplate;

\tpublic void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
\t\tthis.jdbcTemplate = jdbcTemplate;
\t}

\t// 對數據操作的方法不包含業務操作

\t/**
\t * 小王少錢的方法
\t */
\tpublic void reduceMoney() {
\t\tString sql = "update account set salary=salary-? where username=?";
\t\tjdbcTemplate.update(sql, 1000, "小王");
\t}

\t/**
\t * 小馬多錢的方法
\t */
\tpublic void addMoney() {
\t\tString sql = "update account set salary=salary+? where username=?";
\t\tjdbcTemplate.update(sql, 1000, "小馬");
\t}
}

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 {
\t// 注入Dao層對象
\tprivate OrdersDao ordersDao;

\tpublic void setOrdersDao(OrdersDao ordersDao) {
\t\tthis.ordersDao = ordersDao;
\t}

\t// 注入TransactionTemplate對象
\tprivate TransactionTemplate transactionTemplate;

\tpublic void setTransactionTemplate(TransactionTemplate transactionTemplate) {
\t\tthis.transactionTemplate = transactionTemplate;
\t}

\t// 調用dao的方法
\t// 業務邏輯,寫轉賬業務

\tpublic void accountMoney() {
\t\ttransactionTemplate.execute(new TransactionCallback<object>() {

\t\t\t@Override
\t\t\tpublic Object doInTransaction(TransactionStatus status) {
\t\t\t\tObject result = null;
\t\t\t\ttry {
\t\t\t\t\t// 小馬多1000
\t\t\t\t\tordersDao.addMoney();
\t\t\t\t\t// 加入出現異常如下面int
\t\t\t\t\t// i=10/0(銀行中可能為突然停電等。。。);結果:小馬賬戶多了1000而小王賬戶沒有少錢
\t\t\t\t\t// 解決辦法是出現異常後進行事務回滾
\t\t\t\t\tint i = 10 / 0;// 事務管理配置後異常已經解決
\t\t\t\t\t// 小王 少1000
\t\t\t\t\tordersDao.reduceMoney();
\t\t\t\t} catch (Exception e) {
\t\t\t\t\tstatus.setRollbackOnly();
\t\t\t\t\tresult = false;
\t\t\t\t\tSystem.out.println("Transfer Error!");
\t\t\t\t}

\t\t\t\treturn result;
\t\t\t}
\t\t});

\t}
}
/<object>

TestService.java(測試方法)

package cn.itcast.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestService {
\t@Test
\tpublic void testAdd() {

\t\tApplicationContext context = new ClassPathXmlApplicationContext(
\t\t\t\t"beans.xml");
\t\tOrdersService userService = (OrdersService) context
\t\t\t\t.getBean("ordersService");
\t\tuserService.accountMoney();
\t}
}

配置文件:


<beans>\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
\txmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
\txsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
\t
\t<bean>
\t\t
\t\t<property>
\t\t<property>
\t\t<property>
\t\t<property>
\t/<bean>
\t
\t
\t<bean>\t\tclass="org.springframework.jdbc.datasource.DataSourceTransactionManager">
\t\t
\t\t<property>
\t/<bean>

\t
\t<bean>\t\tclass="org.springframework.transaction.support.TransactionTemplate">
\t\t

\t\t<property>
\t/<bean>

\t
\t<bean>
\t\t<property>
\t\t
\t\t<property>
\t/<bean>

\t<bean>
\t\t<property>
\t/<bean>
\t
\t<bean>
\t\t<property>
\t/<bean>
/<beans>

(2)基於AspectJ的聲明式事務管理

OrdersService.java(業務邏輯層)

package cn.itcast.service;

import cn.itcast.dao.OrdersDao;

public class OrdersService {
\tprivate OrdersDao ordersDao;

\tpublic void setOrdersDao(OrdersDao ordersDao) {
\t\tthis.ordersDao = ordersDao;
\t}

\t// 調用dao的方法
\t// 業務邏輯,寫轉賬業務
\tpublic void accountMoney() {

\t\t// 小馬多1000
\t\tordersDao.addMoney();
\t\t// 加入出現異常如下面int i=10/0(銀行中可能為突然停電等。。。);結果:小馬賬戶多了1000而小王賬戶沒有少錢
\t\t// 解決辦法是出現異常後進行事務回滾
\t\tint i = 10 / 0;// 事務管理配置後異常已經解決
\t\t// 小王 少1000
\t\tordersDao.reduceMoney();
\t}
}

配置文件:


\t<bean>
\t\t
\t\t<property>
\t\t<property>
\t\t<property>
\t\t<property>
\t/<bean>
\t
\t<bean>\t\tclass="org.springframework.jdbc.datasource.DataSourceTransactionManager">
\t\t
\t\t<property>
\t/<bean>

\t
\t<advice>
\t\t

\t\t<attributes>
\t\t\t
\t\t\t
\t
\t\t\t<method>\t\t\t\tisolation="DEFAULT" read-only="false" rollback-for="" timeout="-1" />
\t\t/<method>/<attributes>
\t/<advice>

\t
\t<config>
\t\t
\t\t<pointcut>\t\t\tid="pointcut1" />
\t\t
\t\t<advisor>
\t/<pointcut>/<config>


\t
\t<bean>
\t\t<property>
\t/<bean>
\t<bean>
\t\t<property>
\t/<bean>
\t<bean>
\t\t<property>
\t/<bean>

(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 {
\tprivate OrdersDao ordersDao;

\tpublic void setOrdersDao(OrdersDao ordersDao) {
\t\tthis.ordersDao = ordersDao;
\t}

\t// 調用dao的方法
\t// 業務邏輯,寫轉賬業務
\tpublic void accountMoney() {
\t\t// 小馬多1000
\t\tordersDao.addMoney();
\t\t// 加入出現異常如下面int i=10/0(銀行中可能為突然停電等。。。);結果:小馬賬戶多了1000而小王賬戶沒有少錢
\t\t// 解決辦法是出現異常後進行事務回滾
\t\t// int i = 10 / 0;// 事務管理配置後異常已經解決
\t\t// 小王 少1000
\t\tordersDao.reduceMoney();
\t}
}

配置文件:


\t<bean>
\t\t
\t\t<property>
\t\t<property>
\t\t<property>
\t\t<property>
\t/<bean>
\t
\t<bean>\t\tclass="org.springframework.jdbc.datasource.DataSourceTransactionManager">
\t\t
\t\t<property>
\t/<bean>
\t
\t<annotation-driven>
\t
\t
\t
\t
\t<bean>
\t\t<property>
\t/<bean>
\t<bean>
\t\t<property>
\t/<bean>
\t<bean>
\t\t<property>
\t/<bean>

歡迎關注我的微信公眾號:
"漫談Java架構" (一個有溫度的微信公眾號,期待與你共同進步~~~堅持原創,分享美文,分享各種Java學習資源):需要jar包,私信回覆jar


分享到:


相關文章: