@Transactional 使用注意點
- @Transactional 注解隻能應用到 public 方法或者類上才有效
- 首先開啟 @EnableTransactionManagement,但是Spring Boot已經預設開啟 @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
@Transactional 的簡單使用
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 異常該事務進行了復原,資料庫中插入資料失敗。
@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 "成功";
}
- save 方法無 @Transactional 注解
- save 方法有 @Transactional 注解