spring教程--事务管理

Spring的事务管理

1.1 事务:

事务:是逻辑上一组操作,要么全都成功,要么全都失败.

事务特性:

ACID:

原子性:事务不可分割

一致性:事务执行的前后,数据完整性保持一致.

隔离性:一个事务执行的时候,不应该受到其他事务的打扰

持久性:一旦结束,数据就永久的保存到数据库.

如果不考虑隔离性:

脏读:一个事务读到另一个事务未提交数据

不可重复读:一个事务读到另一个事务已经提交数据(update)导致一个事务多次查询结果不一致

虚读:一个事务读到另一个事务已经提交数据(insert)导致一个事务多次查询结果不一致

事务的隔离级别:

未提交读:以上情况都有可能发生。

已提交读:避免脏读,但不可重复读,虚读是有可能发生。

可重复读:避免脏读,不可重复读,但是虚读有可能发生。

串行的:避免以上所有情况.

1.2 Spring中事务管理:

分层开发:事务处在Service层.

Spring提供事务管理API:

PlatformTransactionManager:平台事务管理器.

PROPAGATION_XXX:事务的传播行为.(不是JDBC中有的,为了解决实际开发问题.)

过期时间:

TransactionStatus:事务状态

是否有保存点

是否一个新的事务

事务是否已经提交

关系:PlatformTransactionManager通过TransactionDefinition设置事务相关信息管理事务,管理事务过程中,产生一些事务状态:状态由TransactionStatus记录.

API详解:

PlatformTransactionManager:接口.

Spring为不同的持久化框架提供了不同PlatformTransactionManager接口实现

org.springframework.jdbc.datasource.DataSourceTransactionManager:使用Spring JDBC或iBatis 进行持久化数据时使用

org.springframework.orm.hibernate3.HibernateTransactionManager: 使用Hibernate3.0版本进行持久化数据时使用

org.springframework.orm.jpa.JpaTransactionManager使用JPA进行持久化时使用

org.springframework.jdo.JdoTransactionManager当持久化机制是Jdo时使用

org.springframework.transaction.jta.JtaTransactionManager使用一个JTA实现来管理事务,在一个事务跨越多个资源时必须使用

TransactionDefinition:

* ISOLATION_DEFAULT:默认级别. Mysql  repeatable_readoracle read_commited

* 事务的传播行为:(不是JDBC事务管理,用来解决实际开发的问题.)传播行为:解决业务层之间的调用的事务的关系.

PROPAGATION_REQUIRED:支持当前事务,如果不存在 就新建一个

* A,B如果A有事务,B使用A的事务,如果A没有事务,B就开启一个新的事务.(A,B是在一个事务中。)

PROPAGATION_SUPPORTS:支持当前事务,如果不存在,就不使用事务

* A,B如果A有事务,B使用A的事务,如果A没有事务,B就不使用事务.

PROPAGATION_MANDATORY:支持当前事务,如果不存在,抛出异常

* A,B如果A有事务,B使用A的事务,如果A没有事务,抛出异常.

PROPAGATION_REQUIRES_NEW:如果有事务存在,挂起当前事务,创建一个新的事务

* A,B如果A有事务,B将A的事务挂起,重新创建一个新的事务.(A,B不在一个事务中.事务互不影响.)

PROPAGATION_NOT_SUPPORTED:以非事务方式运行,如果有事务存在,挂起当前事务

* A,B非事务的方式运行,A有事务,就会挂起当前的事务.

PROPAGATION_NEVER: 以非事务方式运行,如果有事务存在,抛出异常

PROPAGATION_NESTED: 如果当前事务存在,则嵌套事务执行

* 基于SavePoint技术.

* A,BA有事务,A执行之后,将A事务执行之后的内容保存到SavePoint.B事务有异常的话,用户需要自己设置事务提交还是回滚.

常用:

PROPAGATION_REQUIRED

PROPAGATION_REQUIRES_NEW

PROPAGATION_NESTED

1.3 Spring的事务管理:

Spring的事务管理分成两类:

* 编程式事务管理:

* 手动编写代码完成事务管理.

* 声明式事务管理:

* 不需要手动编写代码,配置.

1.4 事务操作的环境搭建:

CREATE TABLE `account` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(20) NOT NULL,

  `money` double DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO `account` VALUES ('1', 'aaa', '1000');

INSERT INTO `account` VALUES ('2', 'bbb', '1000');

INSERT INTO `account` VALUES ('3', 'ccc', '1000');

1.4.1 创建一个web项目:

* 导入相应jar包

* 引入配置文件:

* applicationContext.xml、log4j.properties、jdbc.properties

1.4.2 创建类:

* AccountService

* AccountDao

1.4.3 在Spring中注册:

<!-- 业务层类 -->

<bean id="accountService" class="com.sihai.spring3.demo1.AccountServiceImpl">

<!-- 在业务层注入Dao -->

<property name="accountDao" ref="accountDao"/>

</bean>

<!-- 持久层类 -->

<bean id="accountDao" class="com.sihai.spring3.demo1.AccountDaoImpl">

<!-- 注入连接池的对象,通过连接池对象创建模板. -->

<property name="dataSource" ref="dataSource"/>

</bean>

1.4.4 编写一个测试类:

package com.sihai.spring3.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 {
	@Autowired
	@Qualifier("accountService")
	private AccountService accountService;
	
	@Test
	public void demo1(){
		// 完成转账
		accountService.transfer("aaa", "bbb", 100d);
	}
}

1.5 Spring的事务管理

1.5.1 手动编码的方式完成事务管理:

需要事务管理器:真正管理事务对象.

* Spring提供了事务管理的模板(工具类.)

第一步:注册事务管理器:

<!-- 配置事务管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!-- 需要注入连接池,通过连接池获得连接 -->

<property name="dataSource" ref="dataSource"/>

</bean>

第二步:注册事务模板类:

<!-- 事务管理的模板 -->

<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">

<property name="transactionManager" ref="transactionManager"/>

</bean>

第三步:在业务层注入模板类:(模板类管理事务)

<!-- 业务层类 -->

<bean id="accountService" class="com.sihai.spring3.demo1.AccountServiceImpl">

<!-- 在业务层注入Dao -->

<property name="accountDao" ref="accountDao"/>

<!-- 在业务层注入事务的管理模板 -->

<property name="transactionTemplate" ref="transactionTemplate"/>

</bean>

第四步:在业务层代码上使用模板:

public void transfer(final String from, final String to, final Double money) {

transactionTemplate.execute(new TransactionCallbackWithoutResult() {

@Override

protected void doInTransactionWithoutResult(TransactionStatus status) {

accountDao.out(from, money);

int d = 1 / 0;

accountDao.in(to, money);

}

});

}

1.5.2 声明式事务管理:(原始方式)

基于TransactionProxyFactoryBean.

导入:aop相应jar包.

第一步:注册平台事务管理器:

<!-- 事务管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!-- 注入连接池 -->

<property name="dataSource" ref="dataSource"/>

</bean>

第二步:创建业务层代理对象:

<!-- 配置生成代理对象 -->

<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<!-- 目标对象 -->

<property name="target" ref="accountService"/>

<!-- 注入事务管理器 -->

<property name="transactionManager" ref="transactionManager"/>

<!-- 事务的属性设置 -->

<property name="transactionAttributes">

<props>

<prop key="transfer">PROPAGATION_REQUIRED</prop>

</props>

</property>

</bean>

第三步:编写测试类:

package com.sihai.spring3.demo2;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class SpringTest2 {

	@Autowired
	@Qualifier("accountServiceProxy")
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("aaa", "bbb", 100d);
	}
}

1.5.3 声明式事务管理:(自动代理.基于切面)

第一步:导入相应jar包.

 aspectj

第二步:引入相应约束:

 aop、tx约束.

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="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.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

" >http://www.springframework.org/schema/tx/spring-tx.xsd">

第三步:注册事务管理器;

<!-- 事务管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

第四步:定义增强(事务管理)

<!-- 定义一个增强 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<!-- 增强(事务)的属性的配置 -->

<tx:attributes>

<!--

isolation:DEFAULT:事务的隔离级别.

propagation:事务的传播行为.

read-only:false.不是只读

timeout:-1

no-rollback-for:发生哪些异常不回滚

rollback-for:发生哪些异常回滚事务

 -->

<tx:method name="transfer"/>

</tx:attributes>

</tx:advice>

第五步:定义aop的配置(切点和通知的组合)

<!-- aop配置定义切面和切点的信息 -->

<aop:config>

<!-- 定义切点:哪些类的哪些方法应用增强 -->

<aop:pointcut expression="execution(* com.sihai.spring3.demo3.AccountService+.*(..))" id="mypointcut"/>

<!-- 定义切面: -->

<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>

</aop:config>

第六步:编写测试类:

 注入Service对象,不需要注入代理对象(生成这个类的时候,已经是代理对象.)

package com.sihai.spring3.demo3;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 声明式事务使用:基于切面的
 * @author sihai
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class SpringTest3 {

	@Autowired
	@Qualifier("accountService")
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("aaa", "bbb", 100d);
	}
}

1.5.4 基于注解的事务管理:

第一步:事务管理器:

<!-- 事务管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

第二步:注解事务:

<!-- 开启注解的事务管理 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

第三步:在Service上使用注解

package com.sihai.spring3.demo4;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
public class AccountServiceImpl implements AccountService {
	private AccountDao accountDao;

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	/**
	 * 转账的方法
	 * 
	 * @param from
	 *            :从哪转出
	 * @param to
	 *            :转入的人
	 * @param money
	 *            :转账金额
	 */
	public void transfer(final String from, final String to, final Double money) {
		accountDao.out(from, money);
		//int d = 1 / 0;
		accountDao.in(to, money);
	}
}

第四步:编写测试类

package com.sihai.spring3.demo4;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 声明式事务使用:基于切注解的
 * @author sihai
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext4.xml")
public class SpringTest4 {

	@Autowired
	@Qualifier("accountService")
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("aaa", "bbb", 100d);
	}
}

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello-java-maker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值