Spring框架筆記(二十五)——強大的事務管理

事務管理是企業級應用程序開發中必不可少的技術,  用來確保數據的完整性和一致性. java

事務就是一系列的動做, 它們被當作一個單獨的工做單元. 這些動做要麼所有完成, 要麼所有不起做用mysql

舉個例子來講,我向西大大匯款500,這實際上是一個事務,擔這一個事務包含了兩個操做:個人帳戶減掉500,西大大的帳戶加上500.兩個動做要麼都完成,要麼都不完成。不然數據就不一致了!web


事務的四個關鍵屬性(ACID)spring

原子性(atomicity): 事務是一個原子操做, 由一系列動做組成. 事務的原子性確保動做要麼所有完成要麼徹底不起做用.sql

一致性(consistency): 一旦全部事務動做完成, 事務就被提交. 數據和資源就處於一種知足業務規則的一致性狀態中.數據庫

隔離性(isolation): 可能有許多事務會同時處理相同的數據, 所以每一個事物都應該與其餘事務隔離開來, 防止數據損壞.apache

持久性(durability): 一旦事務完成, 不管發生什麼系統錯誤, 它的結果都不該該受到影響. 一般狀況下, 事務的結果被寫到持久化存儲器中.編程

(本文出自happBKs的博文:http://my.oschina.net/happyBKs/blog/513375)服務器

好,咱們在看看開發過程當中咱們會遇到什麼問題。mvc

加入如今有個數據交互的代碼,它包含了數據庫操做的最基本的步驟,一開始的獲取鏈接,設置是否自動提交事務;

而後編寫不一樣過得業務代碼;而後提交事務,若是發生異常,就進行回滾;最後,不管事務執行如何,關閉鏈接等等。

這些套數據庫交互的邏輯須要被衆多業務所使用,也就是說,不管咱們的功能是什麼,都須要按照這樣的步驟寫代碼。顯然這樣作將是一個麻煩且不適於維護的事情。

問題: 

必須爲不一樣的方法重寫相似的樣板代碼

這段代碼是特定於 JDBC 的, 一旦選擇類其它數據庫存取技術, 代碼須要做出相應的修改



如今,咱們仔細審視這段代碼,是否以爲這些「步驟」彷佛與AOP的各個通知可以對應起來。

如前面的獲取鏈接和開啓事務,至關於前置通知;提交事務至關於返回通知;回滾事務至關於異常通知;finally至關於後置通知。因此咱們能夠將其做爲AOP處理事務的基本原理或者說出發點。


做爲企業級應用程序框架, Spring 在不一樣的事務管理 API 之上定義了一個抽象層. 而應用程序開發人員沒必要了解底層的事務管理 API, 就可使用 Spring 的事務管理機制.

Spring有兩種管理事務的方法:

Spring 既支持編程式事務管理, 也支持聲明式的事務管理. 

編程式事務管理,即經過寫代碼的方式來實現。

聲明式的事務管理,即經過配置的方式實現事務管理。


編程式事務管理: 將事務管理代碼嵌入到業務方法中來控制事務的提交和回滾. 在編程式管理事務時, 必須在每一個事務操做中包含額外的事務管理代碼. 

聲明式事務管理: 大多數狀況下比編程式事務管理更好用. 它將事務管理代碼從業務方法中分離出來, 以聲明的方式來實現事務管理. 事務管理做爲一種橫切關注點, 能夠經過 AOP 方法模塊化. Spring 經過 Spring AOP 框架支持聲明式事務管理.


Spring 從不一樣的事務管理 API 中抽象了一整套的事務機制. 開發人員沒必要了解底層的事務 API, 就能夠利用這些事務機制. 有了這些事務機制, 事務管理代碼就能獨立於特定的事務技術了.簡單的說,好比數據庫交互,Spring的事務管理可以幫助咱們以統一的方式管理JDBC、Mybtis、Hibernate的事務。

Spring 的核心事務管理抽象是 PlatformTransanctionManager它爲事務管理封裝了一組獨立於技術的方法. 不管使用 Spring 的哪一種事務管理策略(編程式或聲明式), 事務管理器都是必須的.

但對於不一樣的事務,好比JDBC、Mybtis、Hibernate的事務,Spring的有着具體的事務管理器。

DataSourceTransanctionManager:在應用程序中只須要處理一個數據源, 並且經過 JDBC 存取

JtaTransanctionManager: 在 JavaEE 應用服務器上用 JTA(Java Transaction API) 進行事務管理

HibernateTransanctionManager:用 Hibernate 框架存取數據庫

……

事務管理器以普通的 Bean 形式聲明在 Spring IOC 容器中.


咱們仍是用一個例子來看看吧。

咱們有下面一個需求,類圖以下:

首先,我在MySQL中建立各個表:

use springjdbc;
create table book(
       isbn varchar(50) primary key,
       book_name varchar(100),
       price int
);

create table book_stock(
       isbn varchar(50) primary key,
       stock int,
       check(stock > 0)       
);

create table account(
       username varchar(50) primary key,
       balance int,
       check(balance > 0)
);


而後,咱們插入一些數據。


好,如今咱們來編寫一下這個買書的代碼:

首先,咱們按照上面的類圖編寫接口及其實現:

package com.happBKs.spring.tx;

public interface BookShopDao {
	//根據書號獲取書的單價
	public int findBookPriceIsdn(String isbn);
	
	//更新書的庫存,使得書號對應的庫存-1
	public void updateBookStock(String isbn);
	
	//更新用戶的帳戶餘額:使得相應username記錄的balance-price
	public void updateUserAccount(String username,int price);
}
package com.happBKs.spring.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("bookShopDao")
public class BookShopDaoImpl implements BookShopDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public int findBookPriceIsdn(String isbn) {
		// TODO Auto-generated method stub
		String sql="select price from book where isbn = ?";
		return jdbcTemplate.queryForObject(sql, Integer.class,isbn);
	}

	public void updateBookStock(String isbn) {
		String sql0="select stock from book_stock where isbn = ?";
		int stock=jdbcTemplate.queryForObject(sql0,Integer.class, isbn);
		if(stock==0)
		{
			throw new RuntimeException("庫存不足!");
		}
		// TODO Auto-generated method stub
		String sql="update book_stock set stock = stock-1 where isbn = ?";
		jdbcTemplate.update(sql,isbn);
	}

	public void updateUserAccount(String username, int price) {
		String sql0="select balance from account where username = ?";
		int balance=jdbcTemplate.queryForObject(sql0,Integer.class, username);
		if(balance<price)
		{
			throw new RuntimeException("餘額不足!");
		}
		
		// TODO Auto-generated method stub
		String sql="update account set balance = balance - ? where username = ?";
		jdbcTemplate.update(sql,price,username);
	}

}

注意,上面的方法void updateBookStock(String isbn)中若是庫存爲0了,還要購買則會拋出異常。

void updateUserAccount(String username, int price)中若是用戶帳戶金額不足以購買,也會拋出異常。

package com.happBKs.spring.tx;

public interface BookShopService {
	public void purchase(String username,String isbn);
	
}
package com.happBKs.spring.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("bookShopService")
public class BookShopServiceImpl implements BookShopService {

	@Autowired
	private BookShopDao bookShopDao;
	
	public void purchase(String username, String isbn) {
		// TODO Auto-generated method stub
		//1. 獲取書的單價
		int price=bookShopDao.findBookPriceIsdn(isbn);
		
		
		//2. 更新書的庫存
		bookShopDao.updateBookStock(isbn);
		
		
		//3. 更新用戶餘額
		bookShopDao.updateUserAccount(username, price);
	}

}

咱們在上次的spring jdbc文章中的例子的基礎上修改spring容器配置文件applicationContext.xml。咱們這裏將註解自動掃描的包,設置上今天例子中的包com.happBKs.spring.tx。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

	<!-- 自動掃描的包 -->
	<context:component-scan base-package="com.happBKs.spring.jdbcSpring"></context:component-scan>
	<context:component-scan base-package="com.happBKs.spring.tx"></context:component-scan>


	<!-- 導入資源文件 -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 配置c3p0數據源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>

接下來,編寫幾個測試方法:

package com.happBKs.spring.tx;

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


public class TransactionTest {

	private ApplicationContext ctx=null;
	private BookShopDao bookShopDao;
	private BookShopService bookShopService;
	
	{
		ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		bookShopDao=ctx.getBean(BookShopDao.class);
		bookShopService=ctx.getBean(BookShopService.class);
	}
	
	@Test
	public void test1(){
		System.out.println(bookShopDao.findBookPriceIsdn("1001"));
	}
	
	@Test
	public void test2(){
		bookShopDao.updateBookStock("1001");
	}
	
	@Test
	public void test3(){
		bookShopDao.updateUserAccount("HappyBKs", 100);
	}
	
	@Test
	public void test4(){
		bookShopService.purchase("HappyBKs", "1001");
	}
}

好吧,咱們直接看最後一個test4方法,這個是BookShopService的買東西方法的測試。

一切看起來都是那麼美,好,咱們再買一次。在運行一次test4

這時候,拋異常了,由於執行的第三步餘額不足。

可是問題出現了,以前的庫存已經被扣掉了,然後面由於用戶錢不夠錢確沒少。這樣,若是京東這樣設計的話,估計要被窮屌絲們坑的庫存爆滿但系統缺貨了。

這就是事務管理所要解決的問題,讓一箇中途異常的事務可以回滾。

好吧,咱們如今來看看Spring是如何管理事務的。

咱們首先更新spring 容器配置文件applicationContext.xml:

注意這裏須要加上新的namespace,那就是tx。而後在配置文件的最後加上事務管理器的bean和tx:annotation-driven的事務註解啓用。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

	<!-- 自動掃描的包 -->
	<context:component-scan base-package="com.happBKs.spring.jdbcSpring"></context:component-scan>
	<context:component-scan base-package="com.happBKs.spring.tx"></context:component-scan>


	<!-- 導入資源文件 -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 配置c3p0數據源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
	
	<!-- 配置jdbc模板類 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 配置 NamedParameterJdbcTemplate,該對象可使用具名參數。
	但它沒有無參構造器,因此必須爲其制定構造參數,這裏指定的是出c3p0數據源
	-->
	<bean id="namedParameterJdbcTemplate"
		class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
		<constructor-arg ref="dataSource"></constructor-arg>
	</bean>
	
	<!-- 配置事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 啓用事務註解 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

這裏須要注意的是,可能這裏會出現Error occured processing XML 'org/springframework/transaction/event/TransactionalEventListenerFactory'. 的錯誤提示,這說明咱們在搭建spring環境的時候沒有將spring的事務jar包加全。因此,這裏須要加入spring context的jar,因此這裏從新給出maven的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.happBKs.spring</groupId>
	<artifactId>jdbcSpring</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>jdbcSpring</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.36</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.2.1.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.2.1.RELEASE</version>
		</dependency>
	</dependencies>
</project>

而後,咱們在須要指定爲事務的方法上加入事務註解:

package com.happBKs.spring.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("bookShopService")
public class BookShopServiceImpl implements BookShopService {

	@Autowired
	private BookShopDao bookShopDao;
	
	//添加事務註解
	@Transactional
	public void purchase(String username, String isbn) {
		// TODO Auto-generated method stub
		//1. 獲取書的單價
		int price=bookShopDao.findBookPriceIsdn(isbn);
		
		//2. 更新書的庫存
		bookShopDao.updateBookStock(isbn);
		
		//3. 更新用戶餘額
		bookShopDao.updateUserAccount(username, price);
	}

}

好了,咱們再次運行test4方法來買一次,固然是在剛纔實驗的基礎上。

沒錯,屌絲再次沒買成,可是此次已經不會致使庫存無故減小的狀況了,由於做爲一個完整的事務,spring的事務管理已經爲咱們完成了事務回滾及相關操做。是否是很nice!

相關文章
相關標籤/搜索