@Transactional 注解只能应用到 public 方法或者类上才有效
首先开启 @EnableTransactionManagement,但是Spring Boot已经默认开启 @EnableTransactionManagement 。
查看类 TransactionAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
- //
- // Source code recreated from a .class file by IntelliJ IDEA
- // (powered by FernFlower decompiler)
- //
-
- package org.springframework.boot.autoconfigure.transaction;
-
- import java.util.Collection;
- import java.util.stream.Collectors;
- import org.springframework.beans.factory.ObjectProvider;
- import org.springframework.boot.autoconfigure.AutoConfigureAfter;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
- import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration;
- ...
-
- @Configuration
- @ConditionalOnClass({PlatformTransactionManager.class})
- @AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class})
- @EnableConfigurationProperties({TransactionProperties.class})
- public class TransactionAutoConfiguration {
- public TransactionAutoConfiguration() {
- }
-
- ...
-
- @Configuration
- @ConditionalOnBean({PlatformTransactionManager.class})
- @ConditionalOnMissingBean({AbstractTransactionManagementConfiguration.class})
- public static class EnableTransactionManagementConfiguration {
- public EnableTransactionManagementConfiguration() {
- }
-
- @Configuration
- @EnableTransactionManagement(
- proxyTargetClass = true
- )
- @ConditionalOnProperty(
- prefix = "spring.aop",
- name = {"proxy-target-class"},
- havingValue = "true",
- matchIfMissing = true
- )
- public static class CglibAutoProxyConfiguration {
- public CglibAutoProxyConfiguration() {
- }
- }
-
- @Configuration
- @EnableTransactionManagement(
- proxyTargetClass = false
- )
- @ConditionalOnProperty(
- prefix = "spring.aop",
- name = {"proxy-target-class"},
- havingValue = "false",
- matchIfMissing = false
- )
- public static class JdkDynamicAutoProxyConfiguration {
- public JdkDynamicAutoProxyConfiguration() {
- }
- }
- }
-
- ...
- }
Spring Boot 引入了一个事务自动配置类 TransactionAutoConfiguration。该类已经默认添加EnableTransactionManagement
1) 只需在方法加上 @Transactional 注解
- @Override
- @Transactional
- public String testTransactional(boolean flag){
- testMapper.insert(Test);
- if (flag) {
- throw new RuntimeException("run time error");
- }
- return "成功";
- }
flag = true。该事务没有提交 commit,因为遇到 RuntimeException 异常该事务进行了回滚,数据库中插入数据失败。
2) 结合propagation
- @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
- @Override
- public String save(ProductModuleConfig productModuleConfig){
- testMapper.insert(Test);
- try {
- String a = null;
- boolean equals = a.equals("2");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "成功";
- }
空指针异常没有被 try catch:插入数据库操作成功
空指针异常被 try catch:插入数据库操作成功
空指针异常没有被 try catch:插入数据库操作失败,回滚成功
空指针异常被 try catch:插入数据库操作成功,回滚失败