(1)事务是数据库操作最基本单元,逻辑上的一组操作,要么都成功,如果有一个失败所有操作都失败。
(2)典型场景:银行转账。
2、事务四个特性(ACID)(1)原子性
(2)一致性
(3)隔离性
(4)持久性
搭建事务操作环境 1、创建数据库表,添加记录 2、创建Spring配置文件,同上。<?xml version="1.0" encoding="UTF-8"?>
package com.spring5.dao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;@Repositorypublic class AccountDaoImpl implements AccountDao { @Autowired private JdbcTemplate jdbcTemplate; @Override public void addMoney(int id, float money) { String sql = "update account set money=money+? where id=?"; jdbcTemplate.update(sql, money, id); } @Override public void reduceMoney(int id, float money) { String sql = "update account set money=money-? where id=?"; jdbcTemplate.update(sql, money, id); }}
4、创建service,完成对象创建和注入关系package com.spring5.service;import com.spring5.dao.AccountDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class AccountService { @Autowired private AccountDao accountDao; public void transferAccount() { accountDao.reduceMoney(1, 100); accountDao.addMoney(2, 100); }}
5、编写测试方法package com.spring5.test;import com.spring5.service.AccountService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAccount { @Test public void testAccount() { ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); AccountService accountService = context.getBean("accountService", AccountService.class); accountService.transferAccount(); }}
运行结果:控制台+数据库
6、上面代码,如果正常执行没有问题的,但是如果代码执行过程中出现异常,有问题若更改如下:则转账会有问题!
public void transferAccount() { accountDao.reduceMoney(1, 100); //模拟异常 int i = 10 / 0; accountDao.addMoney(2, 100); }
Spring事务管理 1、事务添加到 JavaEE 三层结构里面 Service 层(业务逻辑层) 2、在 Spring 进行事务管理操作1、上面问题如何解决?
使用事务进行解决
2、事务操作过程
public void transferAccount() { try { //1、开启事务 //2、进行业务操作 accountDao.reduceMoney(1, 100); //模拟异常 int i = 10 / 0; accountDao.addMoney(2, 100); //3、若未发生异常,提交事务 } catch (Exception e) { //4、出现异常,事务回滚 } }
有两种方式:编程式事务管理和声明式事务管理(使用)
3、声明式事务管理 (1)基于注解方式(使用) (2)基于 xml 配置文件方式 4、在 Spring 进行声明式事务管理,底层使用 AOP 原理 5、Spring 事务管理 API 提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类。
注解声明式事务管理 1、在 spring 配置文件配置事务管理器
<?xml version="1.0" encoding="UTF-8"?>
(1)@Transactional,这个注解添加到类上面,也可以添加方法上面
(2)如果把这个注解添加类上面,这个类里面所有的方法都添加事务
(3)如果把这个注解添加方法上面,为这个方法添加事务
package com.spring5.service;import com.spring5.dao.AccountDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Service@Transactionalpublic class AccountService { @Autowired private AccountDao accountDao; public void transferAccount() { //try { // //1、开启事务 // //2、进行业务操作 accountDao.reduceMoney(1, 100); //模拟异常 int i = 10 / 0; accountDao.addMoney(2, 100); // // //3、若未发生异常,提交事务 //} catch (Exception e) { // //4、出现异常,事务回滚 //} }}
4、测试声明式事务管理参数配置 1、在 service 类上面添加注解@Transactional,在这个注解里面可以配置事务相关参数 2、propagation:事务传播行为此时,数据库数据保持一致,转账不会出错!
@Transactional(propagation = Propagation.REQUIRED)
多事务方法之间进行调用,这个过程中事务是如何进行管理的
3、isolation:事务隔离级别默认REQUIRED
@Transactional(isolation = Isolation.REPEATABLE_READ)
(1)事务有特性成为隔离性,多事务操作之间不会产生影响。不考虑隔离性产生很多问题
(2)有三个读问题:脏读、不可重复读、虚读 (幻读)
(3)脏读:一个未提交事务读取到另一个未提交事务的数据
(4)不可重复读:一个未提交事务读取到另一提交事务修改数据
(5)虚读:一个未提交事务读取到另一提交事务添加数据
(6)解决:通过设置事务隔离级别,解决读问题
4、timeout:超时时间在MySQL中默认是REPEATABLE_READ
@Transactional(timeout = -1)
(1)事务需要在一定时间内进行提交,如果不提交进行回滚
(2)默认值是 -1 ,设置时间以秒单位进行计算
5、readOnly:是否只读@Transactional(readonly = false)
(1)读:查询操作,写:添加修改删除操作
(2)readonly 默认值 false,表示可以查询,可以添加修改删除操作
(3)设置 readonly 值是 true,设置成 true 之后,只能查询
6、rollbackFor:回滚 设置出现哪些异常进行事务回滚
7、noRollbackFor:不回滚 设置出现哪些异常不进行事务回滚
XML声明式事务管理 第一步 配置事务管理器 第二步 配置通知 第三步 配置切入点和切面<?xml version="1.0" encoding="UTF-8"?>
先注释掉AccountService类上的@Transactional注解,在测试类中新增测试方法,如下
@Test public void testAccount2() { ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); AccountService accountService = context.getBean("accountService", AccountService.class); accountService.transferAccount(); }
运行结果:数据库数据也正常
完全注解声明式事务管理 创建配置类TxConfig.class,使用配置类替代 xml 配置文件package com.spring5.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;import java.util.ResourceBundle;@Configuration // 配置类@ComponentScan(basePackages = "com.spring5") // 组件扫描@EnableTransactionManagement // 开启事务public class TxConfig { // 创建数据库连接池 @Bean public DruidDataSource getDruidDataSource() { DruidDataSource datasource = new DruidDataSource(); ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); // 属性配置文件扩展名不写 datasource.setDriverClassName(bundle.getString("prop.driverClass")); datasource.setUrl(bundle.getString("prop.url")); datasource.setUsername(bundle.getString("prop.username")); datasource.setPassword(bundle.getString("prop.password")); return datasource; } // 创建JdbcTemplate对象 @Bean public JdbcTemplate getJdbcTemplate(DataSource dataSource) { // 到ioc容器中根据类型找到dataSource JdbcTemplate jdbcTemplate = new JdbcTemplate(); // 注入dataSource jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; } // 创建事务管理器 @Bean public DataSourceTransactionManager getTransactionManager(DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; }}
测试方法:@Test public void testAccount3() { ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class); AccountService accountService = context.getBean("accountService", AccountService.class); accountService.transferAccount(); }
记得恢复AccountService类上的@Transactional注解!然后再执行测试方法,否则数据库数据会变!