Spring事务管理是Spring框架中用于确保数据库操作 原子性、一致性、隔离性和持久性(ACID) 的核心机制。它通过声明式或编程式(本文略)方式管理事务,支持多种事务传播行为和隔离级别
@Transactional注解实现事务管理,无需手动编写事务代码
作用:提供声明式事务管理。它简化了在应用程序中管理数据库事务的流程。开发者只需在方法或类上添加此注解,Spring框架就会自动处理事务的开启、提交和回滚,无需手动编写事务管理代码(如 begin、commit、rollback)级别:类 + 方法
@RequestMapping("/test")
@RestController
@Slf4j
public class TestController {
private final UserService userService;
@Autowired
public TestController(UserService userService) {
this.userService = userService;
}
@Transactional
@RequestMapping("/test1")
public String test1(String userName,String password) {
UserInfo userInfo = new UserInfo();
userInfo.setUserName(userName);
userInfo.setPassword(password);
Integer result = userService.register(userInfo);
if (result == 1){
log.info("test1注册成功,userName:{},password:{}", userName, password);
}
return "注册成功";
}
}使用PostMan向后端发送请求:

MySQL查询结果如下:

后端日志日志如下:

作用:指定哪些异常触发回滚,默认情况下在抛出 非受查异常(RuntimeException)/错误(Error) 时触发回滚

抛出受查异常时
@RequestMapping("/test")
@RestController
@Slf4j
public class TestController {
private final UserService userService;
@Autowired
public TestController(UserService userService) {
this.userService = userService;
}
@Transactional
@RequestMapping("/test2")
public String test2(String userName,String password) throws IOException {
UserInfo userInfo = new UserInfo();
userInfo.setUserName(userName);
userInfo.setPassword(password);
Integer result = userService.register(userInfo);
if (result == 1){
log.info("test2注册成功,userName:{},password:{}", userName, password);
throw new IOException();
}
return "注册成功";
}
}使用PostMan向后端发送请求:

MySQL查询结果如下:

后端日志日志如下:

抛出非受查异常时
@RequestMapping("/test")
@RestController
@Slf4j
public class TestController {
private final UserService userService;
@Autowired
public TestController(UserService userService) {
this.userService = userService;
}
@Transactional
@RequestMapping("/test3")
public String test3(String userName,String password) {
UserInfo userInfo = new UserInfo();
userInfo.setUserName(userName);
userInfo.setPassword(password);
Integer result = userService.register(userInfo);
if (result == 1){
log.info("test3注册成功,userName:{},password:{}", userName, password);
throw new RuntimeException();
}
return "注册成功";
}
}使用PostMan向后端发送请求:

MySQL查询结果如下:

后端日志日志如下:

指定回滚类型
@RequestMapping("/test")
@RestController
@Slf4j
public class TestController {
private final UserService userService;
@Autowired
public TestController(UserService userService) {
this.userService = userService;
}
@Transactional(rollbackFor = Exception.class)
@RequestMapping("/test4")
public String test4(String userName,String password) throws IOException {
UserInfo userInfo = new UserInfo();
userInfo.setUserName(userName);
userInfo.setPassword(password);
Integer result = userService.register(userInfo);
if (result == 1){
log.info("test4注册成功,userName:{},password:{}", userName, password);
throw new IOException();
}
return "注册成功";
}
}使用PostMan向后端发送请求:

MySQL查询结果如下:

后端日志日志如下:

作用:用于指定事务的隔离级别
每种隔离级别的具体效果在全面解析MySQL(5)——“索引、事务、JDBC”三大核心一文中有介绍,本文不再赘述
作用:用于定义事务的传播行为,即当前事务方法被另一个事务方法调用时,事务应如何传播。Spring提供了7种传播行为,均基于Propagation枚举类实现
默认传播行为。如果当前存在事务,则加入该事务;如果不存在事务,则新建一个事务


如果当前存在事务,则加入该事务;如果不存在事务,则以非事务方式执行


强制要求当前存在事务并加入,否则抛出异常


无论当前是否存在事务,都新建一个事务。新事务与当前事务独立,互不干扰


以非事务方式执行操作,如果当前存在事务,则挂起该事务


强制要求当前不能存在事务,否则抛出异常


如果当前存在事务,则在嵌套事务中执行;如果不存在事务,则行为与
Propagation.REQUIRED相同。嵌套事务的回滚不影响外部事务,但外部事务回滚会导致嵌套事务回滚(适用于需要部分回滚的场景)


Gitee地址:九转苍翎 本文源码:spring-trans