一、@Transactional 注解使用

  • @Transactional  注解只能用在public 方法上,如果用在protected或者private的方法上,不会报错,但是该注解不会生效。
  • @Transactional注解只能回滚非检查型异常,具体为RuntimeException及其子类。
  • 使用rollbackFor 属性来定义回滚的异常类型,使用 propagation 属性定义事务的传播行为。如:
 @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
//回滚Exception类的异常,事务的传播行为支持当前事务,如果没有事务,那么会创建一个事务。
  • @Transactional注解不能回滚被try{}catch() 捕获的异

二、 Spring事务的传播行为

    1)PROPAGATION_REQUIRED   :  支持当前事务,如果当前没有事务,则创建一个事务,这是最常见的选择。

2)PROPAGATION_SUPPORTS   :  支持当前事务,如果当前没有事务,就以非事务来执行。

3)PROPAGATION_MANDATORY  :  支持当前事务,如果没有当前事务,就抛出异常。

4)PROPAGATION_REQUIRES_NEW :   新建事务,如果当前存在事务,就把当前事务挂起。

5)PROPAGATION_NOT_SUPPORTED :  以非事务执行操作,如果当前存在事务,则当前事务挂起。

6)PROPAGATION_NEVER :  以非事务方式执行,如果当前存在事务,则抛出异常。

7)PROPAGATION_NESTED :  如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED 类似的操作。

三、开发案例

workflow服务, 设置的事务传播行为为 :REQUIED

payment服务添加注解: 回滚所有的异常。

Fegin:

package com.hand.hcf.app.workflow.client.extraApi;

import com.hand.hcf.app.workflow.approval.dto.CashTransactionDataCreateCO;
import com.hand.hcf.app.workflow.brms.dto.ApprovalDocumentWithValuesCODTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam; import java.util.List; /**
* PaymentInterface
*
* @Auther: zhengbing.zhang
* @Date:2019/8/19
* @remark
*/
@FeignClient(
name = "${hcf.application.payment.name:fec-payment}",
url = "${hcf.application.payment.url:}",
contextId = "PaymentInterface"
)
public interface PaymentInterface { @PostMapping({"/api/implement/payment/cash/transactionData/batch/v2"})
void saveTransactionDatasBatch(@RequestBody List<ApprovalDocumentWithValuesCODTO> cashTransactionDatas); @GetMapping("/api/acp/requisition/header/update/status/by/documentNumber")
void updateDocumentStatusByDocumentNumber(
@RequestParam("documentNumber") String documentNumber
,@RequestParam("status") Integer status); }

测试:

workflow 调用payment服务的saveTransactionDatasBatch方法, 然Payment服务报错,workflow服务收到错误后回滚:

最新文章

  1. instruments usage error specified target process is invalid
  2. Qt *.pro编写一般规则
  3. Redis Installation、Configuration、Program Based On Redis Learning
  4. 我的工具箱之MySql Front 5.3
  5. 3D游戏调研
  6. [Windows] php开发工具,zendstudio13使用方法补丁
  7. SVN linux端配置
  8. excel文件后台代码
  9. ThinkPHP第十五天(setField、setInc、setDec、关联模型)
  10. Git工作流:中心工作流(翻译)
  11. [转]Patching the Mach-o Format the Simple and Easy Way
  12. MySQL数据库的数据备份和恢复(导入和导出)命令操作语法【转】
  13. UVALive 2323 Modular Multiplication of Polynomials(模拟)
  14. .net软件反编译笔记
  15. 自己动手实现mybatis动态sql
  16. dfs_SPFA 判负环
  17. capwap学习笔记——初识capwap(一)(转)
  18. .NET(WinCE、WM)转Android开发——Xamarin和Smobiler对比
  19. Java笔记(三)异常
  20. vue的坑

热门文章

  1. 解决CentOS添加新网卡后找不到网卡配置文件,配置多网卡并设置静态路由
  2. 【JavaScript】JS的坚实基础
  3. MSSQL SQL注入 总结
  4. FZU_DS_2019_SequenceList
  5. 基于EPPlus和NPOI实现的Excel导入导出
  6. 菜鸡的Java笔记 第二十三 - java 抽象类的概念
  7. VS2013中using System.Windows.Forms;引用不成功
  8. [atARC066F]Contest with Drinks Hard
  9. Redis 持久化方案
  10. 论文翻译:2020_A Recursive Network with Dynamic Attention for Monaural Speech Enhancement