SpringBoot配置全局事务管理
⽬录
基于 SpringBoot 4.5
⼀、注解⽅式
1、在SpringBoot的启动类上加⼊
@EnableTransactionManagement
@SpringBootApplication
// 开启事务⽀持
@EnableTransactionManagement
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2、在需要事务⽀持的类(class)或⽅法(method)上,加上注解并设置其属性
/*
* 表明该类(class)或⽅法(method)受事务控制
* @param propagation  设置隔离级别
* @param isolation 设置传播⾏为
* @param rollbackFor 设置需要回滚的异常类,默认为RuntimeException
*/
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class) @Service
public class UserServiceImpl implements UserService {
@Override
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)    public int addUser(User user) {
return userMapper.addUser(user);
}
}
⼆、AOP的⽅式
package fig;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import t.annotation.Configuration;
import ansaction.TransactionDefinition;
import ansaction.TransactionManager;
import ansaction.interceptor.NameMatchTransactionAttributeSource;
import ansaction.interceptor.RollbackRuleAttribute;
import ansaction.interceptor.RuleBasedTransactionAttribute;
import ansaction.interceptor.TransactionInterceptor;
import java.util.Collections;
/
**
* 全局事务配置类
*/
@Aspect
@Configuration
public class TransactionConfig {
// 切⼊点
private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.pro.service.*.*(..))";
// 需要添加事务切⼊的⽅法列表, 使⽤这些前缀的⽅法都会被添加事务
private static final String[] REQUIRED_RULE_TRANSACTION = {"insert*", "create*", "add*", "save*", "update*", "del*", "delete*"};
private static final String[] READ_RULE_TRANSACTION = {"select*", "get*", "query*", "search*", "count*", "find*"};
@Autowired
//    private PlatformTransactionManager transactionManager;
private TransactionManager transactionManager;
@Bean
public TransactionInterceptor txAdvice() {
// 增删改事务
RuleBasedTransactionAttribute REQUIRED = new RuleBasedTransactionAttribute();
// 当抛出设置的对应异常后,进⾏事务回滚(此处设置为“Exception”级别)
REQUIRED.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
// 设置隔离级别(读已提交的数据)
spring frameworkREQUIRED.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
// 设置传播⾏为(存在事务则加⼊其中,不存在则新建事务)
REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
// 只读事务,不做更新操作
RuleBasedTransactionAttribute READONLY = new RuleBasedTransactionAttribute();
// 当抛出设置的对应异常后,进⾏事务回滚(此处设置为“Exception”级别)
READONLY.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
// 设置隔离级别(读已提交的数据)
READONLY.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
// 设置传播⾏为(如果当前存在事务,则加⼊这个事务,如果当前没有事务,就以⾮事务⽅式执⾏)
READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
/
/ 设置事务是否“只读”(⾮必需,只是声明该事务中不会进⾏修改数据库的操作,可减轻由事务造成的数据库压⼒,属于性能优化的推荐配置)        READONLY.setReadOnly(true);
// 事务管理规则,承载需要进⾏事务管理的⽅法名(模糊匹配)及设置的事务管理属性
NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
// 增删改⽅法的前缀
for (String s : REQUIRED_RULE_TRANSACTION) {
source.addTransactionalMethod(s, REQUIRED);
}
// 查询⽅法的前缀
for (String s : READ_RULE_TRANSACTION) {
source.addTransactionalMethod(s, READONLY);
}
// 实例化事务
return new TransactionInterceptor(transactionManager, source);
}
@Bean
public Advisor txAdviceAdvisor() {
// 声明切点要切⼊的⾯
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
// 设置需要被拦截的路径
pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
// 设置切⾯和配置好的事务管理
return new DefaultPointcutAdvisor(pointcut, txAdvice());
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。