事务嵌套
# 在同一个类中调用带有@Transactional注解的方法如何合并一个事务?
举例:
@Service
public class A{
public void action(){
dosome();
}
@Transactional
public void dosome(){
doa.insert(new Object());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
推荐:Spring AOP,AspectJ 对方法进行切面
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency>
1
2
3
4
5
6
7
8
9
10启动类
@SpringBootApplication @EnableAspectJAutoProxy(exposeProxy = true) public class DonngPartsApplication { public static void main(String[] args) { SpringApplication.run(DonngPartsApplication.class, args); } }
1
2
3
4
5
6
7
8
9修改代码
@Service public class A{ public void action(){ ((A) AopContext.currentProxy()).dosome(); } @Transactional public void dosome(){ doa.insert(new Object()); } }
1
2
3
4
5
6
7
8
9
10
11
12