SpringBoot事务管理:声明式事务和编程式事务。将Service层的事务执行结果传递出去
来源:互联网 发布:数控车床怎么编程视频 编辑:程序博客网 时间:2024/05/12 17:22
在SpringBoot中使用事务很简单,这一篇不是为了讲事务,主要是讲一下一个工具类,将需要加入事务的Service层代码作为参数传递出去并返回事务的执行结果(成功和回滚)。
声明式事务
在SpringBoot中声明式事务最常见,就是把需要事务的方法用@Transactional标注一下就行了,这个一般用在Service层。标注后该方法就具备了事务的能力,出错了会自动回滚。
在大部分场景下,该方法已经够用了。
编程式事务
在有些场景下,我们需要获取事务的状态,是执行成功了还是失败回滚了,那么使用声明式事务就不够用了,需要编程式事务。
在SpringBoot中,可以使用两种编程式事务。
一个是TransactionTemplate
看名字就知道,又是一个类似于RedisTemplate的模板类。使用很简单,是一个回调。
transactionTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus transactionStatus) { try { userRepository.save(user); for (int i = 0; i < 10; i++) { Post post = new Post(); if (i == 5) { post.setContent("dddddddddddddddddddddddddddddddddddddddddddd"); } else post.setContent("post" + i); post.setWeight(i); postService.save(post); } } catch (Exception e) { transactionStatus.isRollbackOnly(); e.printStackTrace(); } return null; } });在doIntransaction里做逻辑处理即可。如果出异常了,就执行isRollbackOnly方法进行回滚。感觉这个比较鸡肋,还不如注解事务来的方便,也不能把事务执行结果同步返回回去。
二是TransactionManager
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); try { for (int i = 0; i < 10; i++) { Post post = new Post(); if (i == 5) { post.setContent("dddddddddddddddddddddddddddddddddddddddddddd"); } else post.setContent("post" + i); post.setWeight(i); postService.save(post); } transactionManager.commit(status); } catch (Exception e) { transactionManager.rollback(status); e.printStackTrace(); }这个就可以了,使用这个就可以把事务结果同步返回给调用端了,出异常了就返回false,成功了就true。
我们就基于这种方法来做一个工具类。这个工具类作用是接收一个Service层需要被事务包围的方法为参数,然后给调用端返回事务结果,供调用端根据结果做相应的处理。
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.DefaultTransactionDefinition;import java.util.function.Consumer;/** * Created by admin on 17/6/12. */@Componentpublic class TransactionUtil { @Autowired private PlatformTransactionManager transactionManager; public boolean transact(Consumer consumer) { TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); try { consumer.accept(null); transactionManager.commit(status); return true; } catch (Exception e) { transactionManager.rollback(status); e.printStackTrace(); return false; } }}该类通过接收一个函数型参数,返回该函数的事务执行结果。由于我们仅仅是为了执行Service代码,并不需要改变代码的值,所有consumer.accept(null)即可。
Service举例:
@Servicepublic class TestService { /** * 此处不需要事务,由Transaction控制事务 */ public void doSome(int i) { System.out.println("我是Service层" + i); }}Controller中就可以使用
boolean result = transactionUtil.transact(s -> testService.doSome(1))来获取事务的执行结果了。
该工具是需要获取事务执行结果的封装。
阅读全文
1 1
- SpringBoot事务管理:声明式事务和编程式事务。将Service层的事务执行结果传递出去
- spring事务管理(声明式事务和编程式事务)
- spring事务管理(声明式事务和编程式事务)
- 编程式事务和声明式事务
- 编程式事务和声明式事务
- 声明式事务和编程式事务
- 事务声明声明式事务和编程式事务介绍
- 事务声明声明式事务和编程式事务介绍
- 事务声明声明式事务和编程式事务介绍
- 事务声明声明式事务和编程式事务介绍
- Spring事务管理-编程式事务、声明式事务
- Spring的声明式事务和编程式事务详解
- Spring的 编程式事务和声明式事务
- Spring的声明式事务和编程式事务详解
- spring管理事务声明式事务和编程式事务
- Spring事务管理(编程式事务和申明式事务)
- Spring事务(编程式事务、声明式事务、事务传播)
- 编程式事务 与 声明式事务
- 在centos6环境下安装TensorFlow
- JS数组中元素数字和字符串之间的转换
- jquery选择器整理
- Logstash通过TCP获取log4j日志
- 【面试题18】树的子结构
- SpringBoot事务管理:声明式事务和编程式事务。将Service层的事务执行结果传递出去
- unbuntu16.04安装elasticsearch5
- 锁 Lock
- Android使用FFmpeg 解码H264并播放(一)
- linux创建桌面快捷启动图标
- 输出单向链表中倒数第k个结点
- AutoEventWireup 属性的作用(转载)
- kotlin配置android基本工程
- Python学习笔记-清除变量