spring事务处理:调用一个方法前的事务处理过程

2024-06-10 19:38

本文主要是介绍spring事务处理:调用一个方法前的事务处理过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实际上,在spring的事务中,只要该类被设置为了事务代理:
 
拦截器都会创建一个TransactionInfo 对象:
 
TransactionInfo txInfo = new TransactionInfo(txAttr, method);
 
而且如果 只要被调用的方法设置了事务属性(txAttr),不管是什么属性都会调用:
 
txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));
 
根据该方法的事务属性(definition )的不同,this.transactionManager.getTransaction(txAttr)的返回值会有所不同(代码见AbstractPlatformTransactionManager),具体为以下几种情况:
1.当前没有事务时(即以下代码中的((HibernateTransactionObject) transaction).hasTransaction()返回false),会返回以下几种:
 
 1    //   Check definition settings for new transaction. 
 2        if   (definition.getTimeout()   <   TransactionDefinition.TIMEOUT_DEFAULT)   {
 3     throw   new  InvalidTimeoutException( " Invalid transaction timeout " , definition.getTimeout());
 4   } 

 5   
 6      //   No existing transaction found -> check propagation behavior to find out how to behave. 
 7        if   (definition.getPropagationBehavior()   ==   TransactionDefinition.PROPAGATION_MANDATORY)   {
 8     throw   new  IllegalTransactionStateException(
 9       " Transaction propagation 'mandatory' but no existing transaction found " );
10   } 

11      else     if   (definition.getPropagationBehavior()   ==   TransactionDefinition.PROPAGATION_REQUIRED   || 
12      definition.getPropagationBehavior()   ==   TransactionDefinition.PROPAGATION_REQUIRES_NEW   || 
13         definition.getPropagationBehavior()   ==   TransactionDefinition.PROPAGATION_NESTED)   {
14      if  (debugEnabled)  {
15     logger.debug( " Creating new transaction with name [ "   +  definition.getName()  +   " ] " );
16    } 

17    doBegin(transaction, definition);
18     boolean  newSynchronization  =  ( this .transactionSynchronization  !=  SYNCHRONIZATION_NEVER);
19     return  newTransactionStatus(definition, transaction,  true , newSynchronization, debugEnabled,  null );
20   } 

21       else     {
22     //  Create "empty" transaction: no actual transaction, but potentially synchronization. 
23      boolean  newSynchronization  =  ( this .transactionSynchronization  ==  SYNCHRONIZATION_ALWAYS);
24     return  newTransactionStatus(definition,  null  false , newSynchronization, debugEnabled,  null );
25   } 

26 
2.当前有事务时
 1    private   TransactionStatus handleExistingTransaction(
 2     TransactionDefinition definition, Object transaction,   boolean   debugEnabled)
 3        throws   TransactionException   {
 4  
 5     if  (definition.getPropagationBehavior()  ==  TransactionDefinition.PROPAGATION_NEVER)  {
 6     throw   new  IllegalTransactionStateException(
 7       " Transaction propagation 'never' but existing transaction found " );
 8   } 

 9  
10     if  (definition.getPropagationBehavior()  ==  TransactionDefinition.PROPAGATION_NOT_SUPPORTED)  {
11      if  (debugEnabled)  {
12     logger.debug( " Suspending current transaction " );
13    } 

14    Object suspendedResources  =  suspend(transaction);
15     boolean  newSynchronization  =  ( this .transactionSynchronization  ==  SYNCHRONIZATION_ALWAYS);
16     return  newTransactionStatus(
17      definition,  null ,  false , newSynchronization, debugEnabled, suspendedResources);
18   } 

19  
20     if  (definition.getPropagationBehavior()  ==  TransactionDefinition.PROPAGATION_REQUIRES_NEW)  {
21      if  (debugEnabled)  {
22     logger.debug( " Suspending current transaction, creating new transaction with name [ "   + 
23       definition.getName()  +   " ] " );
24    } 

25    Object suspendedResources  =  suspend(transaction);
26    doBegin(transaction, definition);
27     boolean  newSynchronization  =  ( this .transactionSynchronization  !=  SYNCHRONIZATION_NEVER);
28     return  newTransactionStatus(
29      definition, transaction,  true , newSynchronization, debugEnabled, suspendedResources);
30   } 

31  
32     if  (definition.getPropagationBehavior()  ==  TransactionDefinition.PROPAGATION_NESTED)  {
33      if  ( ! isNestedTransactionAllowed())  {
34      throw   new  NestedTransactionNotSupportedException(
35        " Transaction manager does not allow nested transactions by default -  "   + 
36        " specify 'nestedTransactionAllowed' property with value 'true' " );
37    } 

38      if  (debugEnabled)  {
39     logger.debug( " Creating nested transaction with name [ "   +  definition.getName()  +   " ] " );
40    } 

41      if  (useSavepointForNestedTransaction())  {
42      //  Create savepoint within existing Spring-managed transaction,
43      //  through the SavepointManager API implemented by TransactionStatus.
44      //  Usually uses JDBC 3.0 savepoints. Never activates Spring synchronization. 
45      DefaultTransactionStatus status  = 
46       newTransactionStatus(definition, transaction,  false ,  false , debugEnabled,  null );
47     status.createAndHoldSavepoint();
48      return  status;
49    } 

50      else   {
51      //  Nested transaction through nested begin and commit/rollback calls.
52      //  Usually only for JTA: Spring synchronization might get activated here
53      //  in case of a pre-existing JTA transaction. 
54      doBegin(transaction, definition);
55      boolean  newSynchronization  =  ( this .transactionSynchronization  !=  SYNCHRONIZATION_NEVER);
56      return  newTransactionStatus(definition, transaction,  true , newSynchronization, debugEnabled,  null );
57    } 

58   } 

59 
最后,txInfo被绑定到当前线程上作为当前事务:
 
txInfo.bindToThread()
 
然后,调用实际的目标类的方法并捕捉异常:
 
  try     {
    //  This is an around advice.
   
 //  Invoke the next interceptor in the chain.
   
 //  This will normally result in a target object being invoked. 
 
   retVal  =  invocation.proceed();
  } 

   
  catch   (Throwable ex)   {
    //  target invocation exception 
 
   doCloseTransactionAfterThrowing(txInfo, ex);
    throw  ex;
  } 

   
  finally     {
   doFinally(txInfo);
  } 

  doCommitTransactionAfterReturning(txInfo);
  
  return   retVal;
 }

另外一点,TransactionInfo的newTransactionStatus调用时如果参数的不是null,TransactionInfo.hasTransaction()方法返回true;
 

这篇关于spring事务处理:调用一个方法前的事务处理过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1049080

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

go中的时间处理过程

《go中的时间处理过程》:本文主要介绍go中的时间处理过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 获取当前时间2 获取当前时间戳3 获取当前时间的字符串格式4 相互转化4.1 时间戳转时间字符串 (int64 > string)4.2 时间字符串转时间

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()