Spring 中 @Transactional 不生效的情况:
- 主类没有加 enbale
@SpringBootApplication
@EnableTransactionManagement
//@EnableDiscoveryClient(autoRegister = true)
public class DslabEventApplication {
public static void main(String[] args) {
SpringApplication.run(DslabEventApplication.class, args);
}
}
- @Transactional 注解只能在 RuntimeException 异常的时候回滚, 所以使用 try catch 后捕获到异常所以该注解会失效
解决办法:- 删除 try catch
- 需要在注解上配置
这个配置仅限于 Throwable 异常类及其子类。@Transactional(rollbackFor = Exception.class)