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!

相关文章
相关标签/搜索