一、注解的方式

1. 在Spring boot工程的主入口类中加入注解

// 开启事务支持
@EnableTransactionManagement
  • 1
  • 2

2. 在需要事务支持的服务类(class)或方法(method)上,加上注解并设置其属性

/*
* 表明该类(class)或方法(method)受事务控制
* @param propagation 设置隔离级别
* @param isolation 设置传播行为
* @param rollbackFor 设置需要回滚的异常类,默认为RuntimeException
*/
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、AOP的方式

  • 如图创建一个class,以切面的形式实现事务管理
// 切面
@Aspect
// 表示该类相当于Spring的xml配置文件中的<Beans>
@Configuration
public class TransactionAdviceConfig { /**
* 定义切点路径
*/
private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.personal.test.Satsuki.service.*.*(..))"; @Autowired
private PlatformTransactionManager transactionManager; /**
* @description 事务管理配置
*/
@Bean
public TransactionInterceptor TxAdvice() {
// 事务管理规则,承载需要进行事务管理的方法名(模糊匹配)及设置的事务管理属性
NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource(); // 设置第一个事务管理的模式(适用于“增删改”)
RuleBasedTransactionAttribute transactionAttribute1 = new RuleBasedTransactionAttribute();
// 当抛出设置的对应异常后,进行事务回滚(此处设置为“Exception”级别)
transactionAttribute1.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
// 设置隔离级别(存在事务则加入其中,不存在则新建事务)
transactionAttribute1.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
// 设置传播行为(读已提交的数据)
transactionAttribute1.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); // 设置第二个事务管理的模式(适用于“查”)
RuleBasedTransactionAttribute transactionAttribute2 = new RuleBasedTransactionAttribute();
// 当抛出设置的对应异常后,进行事务回滚(此处设置为“Exception”级别)
transactionAttribute2.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
// 设置隔离级别(存在事务则挂起该事务,执行当前逻辑,结束后再恢复上下文事务)
transactionAttribute2.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
// 设置传播行为(读已提交的数据)
transactionAttribute2.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
// 设置事务是否“只读”(非必需,只是声明该事务中不会进行修改数据库的操作,可减轻由事务造成的数据库压力,属于性能优化的推荐配置)
transactionAttribute2.setReadOnly(true); // 建立一个map,用来储存要需要进行事务管理的方法名(模糊匹配)
Map<String, TransactionAttribute> txMap = new HashMap<>();
txMap.put("insert*", transactionAttribute1);
txMap.put("update*", transactionAttribute1);
txMap.put("delete*", transactionAttribute1);
txMap.put("query*", transactionAttribute2); // 注入设置好的map
source.setNameMap(txMap);
// 实例化事务拦截器
TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
return txAdvice;
} /**
* @description 利用AspectJExpressionPointcut设置切面
*/
@Bean
public Advisor txAdviceAdvisor() {
// 声明切点要切入的面
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
// 设置需要被拦截的路径
pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
// 设置切面和配置好的事务管理
return new DefaultPointcutAdvisor(pointcut, TxAdvice());
}
}

转载 Spring boot中配置事务管理的更多相关文章

  1. Spring Boot中的事务管理

    原文  http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...

  2. Spring Boot 中的事务管理

    希望能在发生异常的时候被回退,这时候就可以使用事务让它实现回退,做法非常简单,我们只需要在test函数上添加@Transactional注解即可. 使用@Transactional注解来声明一个函数需 ...

  3. Spring Boot中的事务管理 隔离级别

    在声明事务时,只需要通过value属性指定配置的事务管理器名即可,例如:@Transactional(value="transactionManagerPrimary"). 除了指 ...

  4. Spring Boot中的事务是如何实现的

    本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天呢!灯塔君跟大家讲: Spring Boot中的事务是如何实现的 1. 概述 一直在用SpringBoot中的@Transactional来做事 ...

  5. 在Spring Boot中配置web app

    文章目录 添加依赖 配置端口 配置Context Path 配置错误页面 在程序中停止Spring Boot 配置日志级别 注册Servlet 切换嵌套服务器 在Spring Boot中配置web a ...

  6. spring的annotation-driven配置事务管理器详解

    http://blog.sina.com.cn/s/blog_8f61307b0100ynfb.html ——————————————————————————————————————————————— ...

  7. Spring Boot2 系列教程(八)Spring Boot 中配置 Https

    https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书 ...

  8. spring boot中配置日志log和热部署

    Java的日志有很多 个人强烈不推荐log4j ,推荐log4j2和logback 在高并发,多线程的环境下log4j1 的性能和log4j2相比可以用junk来形容  对就是junk.log4j2的 ...

  9. spring、spring boot中配置多数据源

    在项目开发的过程中,有时我们有这样的需求,需要去调用别的系统中的数据,那么这个时候系统中就存在多个数据源了,那么我们如何来解决程序在运行的过程中到底是使用的那个数据源呢? 假设我们系统中存在2个数据源 ...

  10. Spring boot中配置HikariCP连接池

    # jdbc_config datasourcespring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasourc ...

随机推荐

  1. 如何修改word默认模板(Normal.dotm)

    背景描述:平时有大量的文字编辑工作要做,其中最繁琐的就是格式问题:为了排版工整.符合要求,在每个word中都要进行大量的更改,如:字体.大小.行距.段前段后间距等......但这其中有很多的重复性工作 ...

  2. 关于uniapp的兼容性的一些问题

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  3. appium-命令行启动appium-server(windows)

    首先找到build\lib\main.js所在位置,我用的工具是everything(推荐一波),比windows自带的搜索强太多 执行命令:node C:\Users\Acer\AppData\Ro ...

  4. 华为云-容器引擎CCE-部署Nginx应用

    环境准备 注册华为云账号 复制下面地址到浏览器地址栏(https://reg.huaweicloud.com/registerui/cn/register.html?fromacct=c76cea9f ...

  5. 马斯克对于CEO职能,发挥人才天赋,激励人才的想法

    Time Interview with Elon Musk, 29 September 2011. Content 1 Have people do be focused on doing usefu ...

  6. vite 设置网络代理

    参考文档:vite 官网.node-http-proxy 完整示例: export default defineConfig({ server: { proxy: { // 字符串简写写法 '/foo ...

  7. ROS入门21讲(5)

    九.服务数据的定义与使用 1.服务模型 2.自定义服务数据 Person.srv string name uint8 sex uint8 age uint8 unknown = 0 uint8 mal ...

  8. H5移动端字体自适应(也适用于宽高)

    原文地址: https://mp.weixin.qq.com/s/hhmav2sbAgb7w17ipVZiTw 方法一:纯css方法, 精确度高,IOS 和 安卓 字体大小同等比例1.1 以16px为 ...

  9. 借用Ultralytics Yolo快速训练一个物体检测器

    借用Ultralytics Yolo快速训练一个物体检测器 同步发表于 https://www.codebonobo.tech/post/14 大约在16/17年, 深度学习刚刚流行时, Object ...

  10. 强化学习:塑造奖励(Shaping reward)

    "塑造奖励"(Shaping reward)是一个主要用于行为心理学和强化学习领域的技术.它通过对目标行为或结果的逐步接近进行强化,逐渐通过奖励越来越接近目标的行为来"塑 ...