下一篇:(十七)Spring6整合JUnit
spring6里程碑版本的仓库
依赖:spring context依赖、junit依赖、log4j2依赖、mysql驱动依赖、德鲁伊连接池依赖、@Resource注解依赖、spring jdbc依赖
log4j2.xml文件放到类路径下。
数据库:账户表t_act

初始数据:

实体类:Account
/*** 简单的账户类*/
public class Account {private Long id;private String actno;private Double balance;public Account() {}public Account(Long id, String actno, Double balance) {this.id = id;this.actno = actno;this.balance = balance;}@Overridepublic String toString() {return "Account{" +"id=" + id +", actno='" + actno + '\'' +", balance=" + balance +'}';}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getActno() {return actno;}public void setActno(String actno) {this.actno = actno;}public Double getBalance() {return balance;}public void setBalance(Double balance) {this.balance = balance;}
}
什么是事务:
事务的四个处理过程:
事务的四个特性:
以银行账户转账为例。两个账户act-001和act-002。act-001账户向act-002账户转账,必须同时成功,或者同时失败。(一个减成功,一个加成功, 这两条update语句必须同时成功,或同时失败。)
连接数据库的技术采用Spring框架的JdbcTemplate。
采用三层架构搭建:
相关依赖、数据库表(t_act)及数据、实体类(Account)
创建dao包,在dao包下创建接口:AccountDao
/*** 专门负责账户信息的CRUD操作,不和业务挂钩*/
public interface AccountDao {/*** 根据账号查询账户信息* @param actno* @return*/Account selectByActno(String actno);/*** 更新账户信息* @param act* @return*/int update(Account act);
}
在dao层下,创建impl包,新建AccountDao的实现类:AccountDaoImpl
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {@Resource(name = "jdbcTemplate")private JdbcTemplate jdbcTemplate;@Overridepublic Account selectByActno(String actno) {String sql = "select * from t_act where actno = ?";Account account = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class), actno);return account;}@Overridepublic int update(Account act) {String sql = "update t_act set balance = ? where actno = ?";int update = jdbcTemplate.update(sql, act.getBalance(), act.getActno());return update;}
}
创建service包,在service包创建接口:AccountService
public interface AccountService {/*** 转账业务* @param fromAct 转出账户* @param toAct 转出账户* @param money 转账金额*/void transfer(String fromAct,String toAct,double money);
}
在service层下创建impl包,编写AccountService的实现类:AccountServiceImpl
@Service("accountService")
public class AccountServiceImpl implements AccountService {@Resource(name = "accountDao")private AccountDao accountDao;@Overridepublic void transfer(String fromActno, String toActno, double money) {//查询转出账户余额是否充足Account fromAccount = accountDao.selectByActno(fromAct);if (fromAccount.getBalance() < money) {//余额不足抛异常throw new RuntimeException("余额不足");}//修改内存中两个对象的余额Account toAccount = accountDao.selectByActno(toAct);fromAccount.setBalance(fromAccount.getBalance() - money);toAccount.setBalance(toAccount.getBalance() + money);//数据库更新int count = accountDao.update(fromAccount);//模拟异常/*String s = null;s.toString();*/count += accountDao.update(toAccount);if (count != 2) {throw new RuntimeException("转账失败");}
}
创建spring.xml:
添加组件扫描、配置jdbcTemplate、配置德鲁伊连接池
@Testpublic void testSpringTx(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");AccountService accountService = applicationContext.getBean("accountService", AccountService.class);try {accountService.transfer("act001","act002",1000);System.out.println("转账成功。。。");}catch (Exception e){e.printStackTrace();}}
运行程序:

数据库数据改变:

把在业务代码里的模拟异常松开:
@Service("accountService")
public class AccountServiceImpl implements AccountService {@Resource(name = "accountDao")private AccountDao accountDao;@Overridepublic void transfer(String fromActno, String toActno, double money) {//查询转出账户余额是否充足Account fromAccount = accountDao.selectByActno(fromAct);if (fromAccount.getBalance() < money) {//余额不足抛异常throw new RuntimeException("余额不足");}//修改内存中两个对象的余额Account toAccount = accountDao.selectByActno(toAct);fromAccount.setBalance(fromAccount.getBalance() - money);toAccount.setBalance(toAccount.getBalance() + money);//数据库更新int count = accountDao.update(fromAccount);//模拟异常String s = null;s.toString();count += accountDao.update(toAccount);if (count != 2) {throw new RuntimeException("转账失败");}
}
再次运行:

数据库表中数据:

数据异常,丢失1千
Spring实现事务的两种方式
Spring对事务的管理底层实现方式是基于AOP实现的。采用AOP的方式进行了封装。所以Spring专门针对事务开发了一套API,API的核心接口如下:

PlatformTransactionManager接口:spring事务管理器的核心接口。在Spring6中它有两个实现:
如果要在Spring6中使用JdbcTemplate,就要使用DataSourceTransactionManager来管理事务。(Spring内置写好了,可以直接用。)
第一步:在spring配置文件中配置事务管理器。
第二步:在spring配置文件中引入tx命名空间。
第三步:在spring配置文件中配置“事务注解驱动器”,开始注解的方式控制事务。
第四步:在service类上或方法上添加@Transactional注解
@Transactional
@Override@Transactionalpublic void transfer(String fromAct, String toAct, double money) {//查询转出账户余额是否充足Account fromAccount = accountDao.selectByActno(fromAct);if (fromAccount.getBalance() < money) {//余额不足抛异常throw new RuntimeException("余额不足");}//修改内存中两个对象的余额Account toAccount = accountDao.selectByActno(toAct);fromAccount.setBalance(fromAccount.getBalance() - money);toAccount.setBalance(toAccount.getBalance() + money);//数据库更新int count = accountDao.update(fromAccount);//模拟异常String s = null;s.toString();count += accountDao.update(toAccount);if (count != 2) {throw new RuntimeException("转账失败");}}
执行测试程序:

虽然出现异常了,再次查看数据库表中数据:

发现数据没有变化,事务起作用了。
我们可以查看一下@Transactional注解源码:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {@AliasFor("transactionManager")String value() default "";@AliasFor("value")String transactionManager() default "";String[] label() default {};Propagation propagation() default Propagation.REQUIRED;Isolation isolation() default Isolation.DEFAULT;int timeout() default -1;String timeoutString() default "";boolean readOnly() default false;Class extends Throwable>[] rollbackFor() default {};String[] rollbackForClassName() default {};Class extends Throwable>[] noRollbackFor() default {};String[] noRollbackForClassName() default {};
}
有很多的属性,我们只关注重点属性:
事务中的重点属性:
什么是事务的传播行为?
例如在service类中有a()方法和b()方法,a()方法上有事务,b()方法上也有事务,当a()方法执行过程中调用了b()方法,事务是如何传递的?合并到一个事务里?还是开启一个新的事务?这就是事务传播行为。
事务传播行为在spring框架中被定义为枚举类型:
public enum Propagation {REQUIRED(0),SUPPORTS(1),MANDATORY(2),REQUIRES_NEW(3),NOT_SUPPORTED(4),NEVER(5),NESTED(6);private final int value;private Propagation(int value) {this.value = value;}public int value() {return this.value;}
}
一共有七种传播行为:
REQUIRED:支持当前事务,如果不存在就新建一个(默认)
没有就新建,有就加入
SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行
有就加入,没有就不管了
MANDATORY:必须运行在一个事务中,如果当前没有事务正在发生,将抛出一个异常
有就加入,没有就抛异常
REQUIRES_NEW:开启一个新的事务,如果一个事务已经存在,则将这个存在的事务挂起
不管有没有,直接开启一个新事务,开启的新事务和之前的事务不存在嵌套关系,之前事务被挂起
NOT_SUPPORTED:以非事务方式运行,如果有事务存在,挂起当前事务
不支持事务,存在就挂起
NEVER:以非事务方式运行,如果有事务存在,抛出异常
不支持事务,存在就抛异常
NESTED:如果当前正有一个事务在进行中,则该方法应当运行在一个嵌套式事务中。被嵌套的事务可以独立于外层事务进行提交或回滚。如果外层事务不存在,行为就像REQUIRED一样。
有事务的话,就在这个事务里再嵌套一个完全独立的事务,嵌套的事务可以独立的提交和回滚。没有事务就和REQUIRED一样。
需要在数据库中新增两个账户,在一个保存方法里调用另一个保存方法。
首先在持久层接口添加方法,并在实现类实现:
AccountDao接口新增方法:
/*** 新增账户信息* @param act* @return*/int insert(Account act);
AccountDaoImpl实现:
@Overridepublic int insert(Account act) {String sql = "insert into t_act values(null,?,?)";int count = jdbcTemplate.update(sql, act.getActno(), act.getBalance());return count;}
在业务层接口添加方法,及实现类实现:
AccountService接口:
/*** 保存账户信息* @param act*/void save(Account act);
新增第二个实现类:AccountServiceImpl2
@Service("accountService2")
public class AccountServiceImpl2 implements AccountService {@Resource(name = "accountDao")private AccountDao accountDao;@Overridepublic void transfer(String fromAct, String toAct, double money) {}@Override@Transactional(propagation = Propagation.REQUIRED)public void save(Account act) {accountDao.insert(act);//模拟异常/*String s = null;s.toString();*/}
}
AccountServiceImpl实现方法:调用AccountServiceImpl2的save方法
@Resource(name = "accountService2")private AccountService accountService;@Override@Transactional(propagation = Propagation.REQUIRED)public void save(Account act) {//先保存act账户accountDao.insert(act);//再保存act2账户Account act2 = new Account();act2.setActno("act-004");act2.setBalance(1000.0);try {accountService.save(act2);//调用AccountServiceImpl2的save方法}catch (Exception e){}}
测试程序:
@Testpublic void testPropagation(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");AccountService accountService = applicationContext.getBean("accountService", AccountService.class);Account account = new Account();account.setActno("act-003");account.setBalance(10000.0);accountService.save(account);}

可以看见在两个SQL语句之间,Participating in existing transaction,翻译为:参与现有交易,实际上就是加入事务了,查看数据库数据:

正常插入。
如果把AccountServiceImpl2模拟注释松开:
@Override@Transactional(propagation = Propagation.REQUIRED)public void save(Account act) {accountDao.insert(act);//模拟异常String s = null;s.toString();}
发生异常了,回滚事务

数据库数据不改变:

如果把AccountServiceImpl2的事务传播类型换成REQUIRES_NEW
@Override//@Transactional(propagation = Propagation.REQUIRED)@Transactional(propagation = Propagation.REQUIRES_NEW)public void save(Account act) {accountDao.insert(act);//模拟异常String s = null;s.toString();}
再次运行:
可以看见在两个SQL语句之间,Suspending current transaction, creating new transaction with name,翻译为:正在挂起当前事务,正在创建名为的新事务
并且事务2发生异常回滚了,然后
Resuming suspended transaction after completion of inner transaction
Initiating transaction commit
翻译为:
内部事务完成,恢复暂停的事务
启动事务提交
也就是说事务1不会受到事务2的影响,并且如果事务2发生异常,进行捕获,也捕获不到,发生异常后会自动回滚,事务1恢复,并且进行往下执行。
查看数据库:只新增了事务1的账户

余下的事务传播行为就不演示了。
数据库中读取数据存在的三大问题:(三大读问题)
事务隔离级别包括四个级别:
表格归纳:
| 隔离级别 | 脏读 | 不可重复读 |
|---|---|---|
| 读未提交 | 有 | 有 |
| 读提交 | 无 | 有 |
| 可重复读 | 无 | 无 |
| 序列化 | 无 | 无 |
隔离级别在spring中也是以枚举类型存在:
public enum Isolation {DEFAULT(-1),READ_UNCOMMITTED(1),READ_COMMITTED(2),REPEATABLE_READ(4),SERIALIZABLE(8);private final int value;private Isolation(int value) {this.value = value;}public int value() {return this.value;}
}
一个service负责插入,一个service负责查询。负责插入的service要模拟延迟。
创建AccountServiceImpl3类,不实现接口了,负责查询:
/*** 演示隔离级别* 3号负责查询,4号负责插入*/
@Service("accountService3")
public class AccountServiceImpl3 {@Resource(name = "accountDao")private AccountDao accountDao;/*** 根据账户查询* @param actno*/@Transactional(isolation = Isolation.READ_UNCOMMITTED)public void getByActno(String actno){Account account = accountDao.selectByActno(actno);System.out.println("账户信息:" + account);}
}
创建AccountServiceImpl4类,不实现接口了,负责插入:
@Service("accountService4")
public class AccountServiceImpl4 {@Resource(name = "accountDao")private AccountDao accountDao;@Transactionalpublic void save(Account act){ accountDao.insert(act);//让程序睡眠一会try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();} }
}
测试程序:
@Testpublic void testIsolation1() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");AccountServiceImpl4 accountService4 = applicationContext.getBean("accountService4", AccountServiceImpl4.class);Account account = new Account();account.setActno("act-003");account.setBalance(50000.0);accountService4.save(account);}@Testpublic void testIsolation2(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");AccountServiceImpl3 accountService3 = applicationContext.getBean("accountService3", AccountServiceImpl3.class);accountService3.getByActno("act-003");}
执行前需要把数据库数据删除到初始数据。

先执行testIsolation1,在执行testIsolation2
testIsolation1:

testIsolation2:读到了未提交的数据

等testIsolation1执行完后查看数据库数据:

余下的隔离级别就不演示了。
事务超时:使用timeout=整数设置,表示超过多少秒如果该事务中所有的DML语句还没有执行完毕的话,最终结果会选择回滚。默认值是-1,表示没有时间限制。
在AccountServiceImpl4里设置事务超时
在最后一条DML语句执行之后让程序睡眠情况:
//@Transactional@Transactional(timeout = 10)//设置超时事务10秒public void save(Account act) throws IOException {accountDao.insert(act);//让程序睡眠一会try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}}
运行测试程序testIsolation1:

事务提交了,睡眠程序没有计入超时时间,查看数据库数据:成功插入数据

如果想让整个方法的所有代码都计入超时时间的话,可以在方法最后一行添加一行无关紧要的DML语句。
在最后一条DML语句执行之前让程序睡眠情况:
//@Transactional@Transactional(timeout = 10)public void save(Account act){try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}accountDao.insert(act);//让程序睡眠一会/*try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}*/}
再次运行测试程序testIsolation1:
睡眠时间计入超时时间,事务超时,进行了回滚,数据库数据:原封不动

使用readOnly = ture|false 配置,默认未false
将当前事务设置为只读事务,在该事务执行过程中只允许select语句执行,delete insert update均不可执行。
该特性的作用是:启动spring的优化策略。提高select语句执行效率。
如果该事务中确实没有增删改操作,建议设置为只读事务。
在AccountServiceImpl4里设置为只读事务
//@Transactional//@Transactional(timeout = 10) @Transactional(readOnly = true)public void save(Account act) throws IOException {/*try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}*/accountDao.insert(act);//让程序睡眠一会/*try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}*/}
运行测试程序testIsolation1:
出现异常,说Connection是只读的,也就是事务是只读的,不允许导致数据修改

数据库数据不变:

使用rollbackFor = {异常1.class,异常2.class,...}进行设置,表示只有发生异常或该异常的子类异常才回滚。默认为空,表示所有异常都回滚。
在AccountServiceImpl4里设置:
//@Transactional//@Transactional(timeout = 10)//@Transactional(readOnly = true)@Transactional(rollbackFor = RuntimeException.class)//只要发生RuntimeException或者这个子类的异常,都回滚public void save(Account act) throws IOException {/*try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}*/accountDao.insert(act);//模拟异常//throw new IOException();//IO异常是编译时异常throw new RuntimeException();//让程序睡眠一会/*try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}*/}
运行测试程序testIsolation1:

数据库数据不变:

把异常改为其他异常,把另一个异常注释松开,注释RuntimeException异常,再次运行程序:

出现异常,但是事务提交了,数据库数据改变:

使用norollbackFor = {异常1.class,异常2.class,...}进行设置,表示发生该异常或该异常的子类异常不回滚,其他异常则回滚,默认为空,表示所有异常都回滚。
还是修改AccountServiceImpl4:
//@Transactional//@Transactional(timeout = 10)//@Transactional(readOnly = true)//@Transactional(rollbackFor = RuntimeException.class)//只要发生RuntimeException或者这个子类的异常,都回滚@Transactional(noRollbackFor = NullPointerException.class)public void save(Account act) throws IOException {/*try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}*/accountDao.insert(act);//模拟异常//throw new IOException();//IO异常是编译时异常//throw new RuntimeException();throw new NullPointerException();//让程序睡眠一会/*try {Thread.sleep(1000 * 20);} catch (InterruptedException e) {e.printStackTrace();}*/}
运行测试程序testIsolation1:

发生了空指针异常,但是事务提交了,数据库数据改变:

把异常改为其他异常,把另一个RuntimeException异常注释松开,注释NullPointerException异常,再次运行程序:

发生异常,但是回滚了,数据库数据不变:

编写一个类来代替配置文件,怎么编写jdbcTemplate、德鲁伊数据源、事务管理器这些Bean呢?
可以在配置文件编写方法使用@Bean注解修饰,Bean的id就使用name属性设置。
Spring框架看到这个@Bean注解的时候,会调用这个注解标注的方法,返回该方法的对象,该对象会自动纳入IoC容器管理。
这个返回值就是Spring容器的Bean对象了,并且这个Bean对象的名字就是name。
这些Bean的属性是怎么注入呢?
简单类型的属性可以使用注解的value进行配置
如果注入非简单类型的话,它是一个Bean,有两种注入方式
一般非简单类型使用第二种设置
@Configuration// 代替Spring配置文件
@ComponentScan("com.bank")// 代替组件扫描
@EnableTransactionManagement //开启事务注解
public class SpringConfig {@Bean(name = "jdbcTemplate")public JdbcTemplate getJdbcTemplate(DataSource dataSource){JdbcTemplate jdbcTemplate = new JdbcTemplate();//两种注入方式//jdbcTemplate.setDataSource(getDruidDataSource());jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}@Bean(name = "dataSource")public DruidDataSource getDruidDataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/mvc");dataSource.setUsername("root");dataSource.setPassword("root");return dataSource;}@Bean(name = "txManager")public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();transactionManager.setDataSource(dataSource);return transactionManager;}
}
测试还是使用账户转账,把异常去掉:
@Testpublic void testNoXML(){ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);AccountService accountService = applicationContext.getBean("accountService", AccountService.class);try {accountService.transfer("act001","act002",1000);System.out.println("转账成功。。。");}catch (Exception e){e.printStackTrace();}}


测试的话还是使用账户转账。
配置步骤:
记得添加aspectj的依赖:
org.springframework spring-aspects 6.0.0-M2
**添加aop的命名空间。**为后面配置通知和切面做准备。
并且配置组件扫描、配置jdbcTemplate、配置德鲁伊连接池、配置事务管理器
配置通知:使用tx:advice标签
在tx:advice标签里面配置通知的相关属性,使用嵌套标签配置
之前说的所有的事务属性都可以在标签以属性的方式配置
如果方法比较多可以采用通配符的方式进行模糊匹配配置
例如 就是save开始发方法都需要进行事务管理
配置切点:使用标签配置
配置切面:等于通知加切点,使用标签配置
将AccountServiceImpl类上的@Transactional注解注释或删除。并且模拟异常
测试程序:
@Testpublic void testXML(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");AccountService accountService = applicationContext.getBean("accountService", AccountService.class);try {accountService.transfer("act001","act002",1000);System.out.println("转账成功。。。");}catch (Exception e){e.printStackTrace();}}

数据库数据:

通过测试可以看到配置XML已经起作用了。