An open API service indexing awesome lists of open source software.

https://github.com/xiaozhiliaoo/spring-practice

Spring事务失效和使用验证. spring feature validation,spring transaction verification project,aop invalid,transaction invalid, propagation level
https://github.com/xiaozhiliaoo/spring-practice

Last synced: about 2 months ago
JSON representation

Spring事务失效和使用验证. spring feature validation,spring transaction verification project,aop invalid,transaction invalid, propagation level

Awesome Lists containing this project

README

        

# Spring Transaction Training
Spring事务特性验证-基于Mysql
====================

# 准备:
安装mysql8(我的环境是windows),建立person和course表,初始化数据,详见[install-mysql-and-load-data](/src/main/resources/windows-install-mysql.md)

# 结论:
两个不同的Service
- 在一个Service内,事务方法之间的嵌套调用,普通方法和事务方法之间的嵌套调用,被嵌套的方法都不会产生事务,只是一个普通方法。可以通过AopContext.currentProxy()获取被嵌套方法的代理对象,从而产生事务。这里产生事务指的是主动产生的事务(Participating in existing transcation),而并非不能处在事务的上下文中。
- 不同的Service内,事务方法之间的嵌套调用,普通方法和事务方法之间的嵌套调用,被嵌套的方法都会产生事务。
- 在一个Service内,事务方法之间的嵌套调用,普通方法和事务方法之间的嵌套调用不会产生事务传播行为,但是并非被嵌套不能工作在事务上下文中。
- 不能被Spring事务加强的方法指的是不能主动产生一个新事务,并不是不能在事务方法里面运行。

事务失效解决方案:
- 拆分Service。
- 使用AopContext.currentProxy()。

# 测试用例:
- `curl localhost:9888/helloApi/first 同一个service里面切入不同方法`
- `curl localhost:9888/helloApi/second 同一个service里面一个方法调用`
- `curl localhost:9888/personApi/parent1-child1 同一Service,事务方法parent调用事务方法child,child事务不生效`
- `curl localhost:9888/personApi/parent2-child2 同一Service,事务方法parent调用事务方法child,child事务不生效`
- `curl localhost:9888/personApi/parent3-child3 同一Service,事务方法parent调用普通方法child,child会在parent事务中`
- `curl localhost:9888/personApi/parent4-child4 同一Service,普通方法parent调用事务方法child,事务方法child事务不生效`
- `curl localhost:9888/personApi/parent5-child5 不同Service,普通方法parent调用事务方法child,事务方法child事务生效`
- `curl localhost:9888/personApi/parent6-child6 不同Service,事务方法parent调用普通方法child,普通方法child事务生效,并和parent在同一事务`
- `curl localhost:9888/personApi/parent7-child7 不同Service,事务方法parent调用新事务法child,事务方法child生效,和parent在不同事务中`
- `curl localhost:9888/personApi/parent8-child8 不同Service,事务方法parent调用普通方法child,child会在parent事务中,child不抛出异常,parent抛出异常,会使得child回滚`
- `curl localhost:9888/personApi/parent9-child9 测试只读事务`
- `curl localhost:9888/personApi/parent11 测试事务注解默认不回滚Exception异常`
- `curl localhost:9888/proxyApi/proxy Java动态代理测试,代理方法内调用普通方法,普通方法不会产生代理行为`

# 参考:
[JDK动态代理给Spring事务埋下的坑!](https://blog.csdn.net/bntx2jsqfehy7/article/details/79040349)

[Spring事务管理嵌套事务详解](https://blog.csdn.net/levae1024/article/details/82998386)