目录

 

一、事务

1.什么是事务

2.事务的特性

3.如果不考虑隔离性引发的安全性问题

4.解决读问题

二、Spring的事务管理的API

1.PlatformTransactionManager:平台事务管理器

2.TransactionDefinition :事务定义信息

3.TransactionStatus:事务的状态

4.事务管理的API的关系:

三、Spring的事务的传播行为

1.Spring的传播行为

四、Spring的事务管理

1.搭建Spring的事务管理的环境

五、Spring的事务管理:一类:编程式事务(需要手动编写代码)

1.第一步:配置平台事务管理器

2.第二步:Spring提供了事务管理的模板类

3.第三步:在业务层上注入事务管理的模板

4.编写事务管理的代码

5.测试

六、Spring的事务管理:二类:声明式事务管理(通过配置实现)---AOP

1.XML方式的声明式事务管理

2.注解方式的声明式事务管理


一、事务

1.什么是事务

  • 事务:逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部失败。

2.事务的特性

  • 原子性:事务不可分割
  • 一致性:事务执行前后数据完整性保持一致
  • 隔离性:一个事务的执行不应该受到其他事务的干扰
  • 持久性:一旦事务结束,数据就持久化到数据库

3.如果不考虑隔离性引发的安全性问题

  • 读问题
    • 脏读 :一个事务读到另一个事务未提交的数据
    • 不可重复读 :一个事务读到另一个事务已经提交的update的数据,导致一个事务中多次查询结果不一致
    • 虚读、幻读 :一个事务读到另一个事务已经提交的insert的数据,导致一个事务中多次查询结果不一致。
  • 写问题
    • 丢失更新

4.解决读问题

  • 设置事务的隔离级别
    • Read uncommitted :未提交读,任何读问题解决不了。
    • Read committed :已提交读,解决脏读,但是不可重复读和虚读有可能发生。
    • Repeatable read :重复读,解决脏读和不可重复读,但是虚读有可能发生。
    • Serializable :解决所有读问题。

二、Spring的事务管理的API

1.PlatformTransactionManager:平台事务管理器

  • 平台事务管理器:接口,是Spring用于管理事务的真正的对象。
    • DataSourceTransactionManager :底层使用JDBC管理事务
    • HibernateTransactionManager :底层使用Hibernate管理事务

2.TransactionDefinition :事务定义信息

  • 事务定义:用于定义事务的相关的信息,隔离级别、超时信息、传播行为、是否只读

3.TransactionStatus事务的状态

  • 事务状态:用于记录在事务管理过程中,事务的状态的对象。

4.事务管理的API的关系:

Spring进行事务管理的时候,首先平台事务管理器根据事务定义信息进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态的对象中。

三、Spring的事务的传播行为

1.Spring的传播行为

  • Spring中提供了七种事务的传播行为:

     PROPAGATION_REQUIRED :默认值,如果A中有事务,使用A中的事务,如果A没有,创建一个新的事务,将操作包含进来

    • 保证多个操作在同一个事务中

    PROPAGATION_SUPPORTS :支持事务,如果A中有事务,使用A中的事务。如果A没有事务,不使用事务。

    PROPAGATION_MANDATORY :如果A中有事务,使用A中的事务。如果A没有事务,抛出异常。

     

    • 保证多个操作不在同一个事务中

    PROPAGATION_REQUIRES_NEW :如果A中有事务,将A的事务挂起(暂停),创建新事务,只包含自身操作。如果A中没有事务,创建一个新事务,包含自身操作。

    PROPAGATION_NOT_SUPPORTED :如果A中有事务,将A的事务挂起。不使用事务管理。

    PROPAGATION_NEVER :如果A中有事务,报异常。

    • 嵌套式事务

      PROPAGATION_NESTED :嵌套事务,如果A中有事务,按照A的事务执行,执行完成后,设置一个保存点,执行B中的操作,如果没有异常,执行通过,如果有异常,可以选择回滚到最初始位置,也可以回滚到保存点。

 

四、Spring的事务管理

1.搭建Spring的事务管理的环境

  • 创建service的接口和实现类
package com.rosinante.tx.deno1;

public interface AccountService {
	public void transfer(String from,String to,Double money);
	
}
package com.rosinante.tx.deno1;

public class AccountServiceImpl implements AccountService{
	
	//注入Dao
	private AccountDao accountDao;
	

	public AccountDao getAccountDao() {
		return accountDao;
	}


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


	/**
	 * from:转出账号
	 * to:转入账号
	 * money:转账金额
	 */
	public void transfer(String from, String to, Double money) {
		// TODO Auto-generated method stub
		accountDao.outMoney(from, money);
		accountDao.inMoney(to, money);
	}
}
  • 创建Dao的接口和实现类
package com.rosinante.tx.deno1;
/**
 * 转账的DAO的接口
 * @author Administrator
 *
 */
public interface AccountDao {
	public void outMoney(String from,Double money);
	public void inMoney(String to,Double money);
}
package com.rosinante.tx.deno1;


import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
	
	

	public void outMoney(String from, Double money) {
		// TODO Auto-generated method stub
		this.getJdbcTemplate().update("update account set money =  money - ? where name = ?",money,from);
	}

	public void inMoney(String to, Double money) {
		// TODO Auto-generated method stub
		this.getJdbcTemplate().update("update account set money =  money + ? where name = ?",money,to);
	}

}
  • 配置Service和Dao:交给spring管理

这里创建一个新的配置文件tx.xml

<!-- 配置Service -->
<bean id="accountService" class="com.rosinante.tx.demo1.AccountServiceImpl">
	<property name="accountDao" ref="accountDao"></property>
</bean>
	
<!-- 配置Dao -->
<bean id="accountDao" class="com.rosinante.tx.demo1.AccountDaoImpl">
	<property name="dataSource" ref="dataSource"></property>
</bean>
  • 在Dao中编写扣钱和加钱方法:

配置连接池和JDBC模板:

<!-- 配置连接池和JDBC模板 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
	
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="${jdbc.driverClass}"/>
	<property name="jdbcUrl" value="${jdbc.url}"/>
	<property name="user" value="${jdbc.username}"/>
	<property name="password" value="${jdbc.password}"/>
</bean>

在Dao中注入JDBC的模板:

<!-- 配置Dao -->
<bean id="accountDao" class="com.rosinante.tx.demo1.AccountDaoImpl">
	<property name="dataSource" ref="dataSource"></property>
</bean>
  • 测试
package com.rosinante.tx.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试转账的环境
 * @author Administrator
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:tx.xml")
public class SpringDemo1 {
	
	@Resource(name="accountService")
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("wangwu", "zhangsan", 1000d);
	}
}

运行结果:

但是,如果转账过程中出现异常,比如我们在AccountServiceImpl类transfer方法中添加一个异常:

public void transfer(String from, String to, Double money) {
	// TODO Auto-generated method stub
	accountDao.outMoney(from, money);
	int d = 1/0;
	accountDao.inMoney(to, money);
}

再次运行:

会发现wangwu转出了1000,但zhangsan并未收到1000。

注:数据库建表请戳上一篇笔记:Spring学习笔记---Spring的JDBC模板的使用

五、Spring的事务管理:一类:编程式事务(需要手动编写代码)

1.第一步:配置平台事务管理器

<!-- 配置平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>

2.第二步:Spring提供了事务管理的模板类

  • 配置事务管理的模板类
<!-- 配置事务管理的模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
	<property name="transactionManager" ref="transactionManager"></property>
</bean>

3.第三步:在业务层上注入事务管理的模板

<!-- 配置Service -->
<bean id="accountService" class="com.rosinante.tx.demo1.AccountServiceImpl">
	<property name="accountDao" ref="accountDao"></property>
	<!-- 注入事务管理模板 -->
	<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>

4.编写事务管理的代码

public void transfer(final String from, final String to, final Double money) {
	// TODO Auto-generated method stub
	transactionTemplate.execute(new TransactionCallbackWithoutResult() {
		
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
			// TODO Auto-generated method stub
			accountDao.outMoney(from, money);
			int d = 1/0;
			accountDao.inMoney(to, money);
		}
	});
}

5.测试

为了方便测试,将表中所有人的money全部改为10000。

将异常语句 int d=1/0;注释掉,执行测试方法demo1():

显示转账成功:

将产生异常的语句 int d=1/0;取消注释,执行测试方法:

转账失败:

六、Spring的事务管理:二类:声明式事务管理(通过配置实现)---AOP

1.XML方式的声明式事务管理

  • 第一步:引入aop的开发包
  • 第二步:恢复转账环境
  • 第三步:配置事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>
  • 第四步:配置增强
<!-- 配置事务增强 -->
<tx:advice transaction-manager="transactionManager">
	<tx:attributes>
		<!-- 事务管理的规则 -->
		<tx:method name="transfer" propagation="REQUIRED"/>
	</tx:attributes>
</tx:advice>
  • 第五步:AOP的配置
<!-- AOP的配置 -->
<aop:config>
	<aop:pointcut expression="execution(* com.rosinante.tx.demo2.AccountServiceImpl.*(..))" id="pointcut1"/>
	<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>

2.注解方式的声明式事务管理

  • 第一步:引入aop的开发包
  • 第二步:恢复转账环境
  • 第三步:配置事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>	
</bean>
  • 第四步:开启注解事务
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
  • 第五步:在业务层上添加注解
package com.rosinante.tx.demo3;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class AccountServiceImpl implements AccountService{
	    
    ......

}

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐