最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring事務(wù)執(zhí)行流程及如何創(chuàng)建事務(wù)

 更新時(shí)間:2021年03月22日 11:29:35   作者:檸檬時(shí)間  
這篇文章主要介紹了Spring事務(wù)執(zhí)行流程及如何創(chuàng)建事務(wù),幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下

接上節(jié)內(nèi)容,Spring事務(wù)執(zhí)行原理通過(guò)創(chuàng)建一個(gè)BeanFactoryTransactionAttributeSourceAdvisor,并把TransactionInterceptor注入進(jìn)去,而TransactionInterceptor實(shí)現(xiàn)了Advice接口。而Spring Aop在Spring中會(huì)把Advisor中的Advice轉(zhuǎn)換成攔截器鏈,然后調(diào)用。

執(zhí)行流程

  1. 獲取對(duì)應(yīng)事務(wù)屬性,也就是獲取@Transactional注解上的屬性
  2. 獲取TransactionManager,常用的如DataSourceTransactionManager事務(wù)管理
  3. 在目標(biāo)方法執(zhí)行前獲取事務(wù)信息并創(chuàng)建事務(wù)
  4. 回調(diào)執(zhí)行下一個(gè)調(diào)用鏈
  5. 一旦出現(xiàn)異常,嘗試異常處理,回滾事務(wù)
  6. 提交事務(wù)

具體分析

獲取對(duì)應(yīng)事務(wù)屬性,具體代碼執(zhí)行流程如下:

final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);
protected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) {
 // Don't allow no-public methods as required.
 //1. allowPublicMethodsOnly()返回true,只能是公共方法
 if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
  return null;
 }

 // Ignore CGLIB subclasses - introspect the actual user class.
 Class<?> userClass = ClassUtils.getUserClass(targetClass);
 // The method may be on an interface, but we need attributes from the target class.
 // If the target class is null, the method will be unchanged.
 //method代表接口中的方法、specificMethod代表實(shí)現(xiàn)類(lèi)的方法
 Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass);
 // If we are dealing with method with generic parameters, find the original method.
 //處理泛型
 specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

 // First try is the method in the target class.
 //查看方法中是否存在事務(wù)
 TransactionAttribute txAttr = findTransactionAttribute(specificMethod);
 if (txAttr != null) {
  return txAttr;
 }

 // Second try is the transaction attribute on the target class.
 //查看方法所在類(lèi)是否存在事務(wù)聲明
 txAttr = findTransactionAttribute(specificMethod.getDeclaringClass());
 if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
  return txAttr;
 }

 //如果存在接口,則在接口中查找
 if (specificMethod != method) {
  // Fallback is to look at the original method.
  //查找接口方法
  txAttr = findTransactionAttribute(method);
  if (txAttr != null) {
   return txAttr;
  }
  // Last fallback is the class of the original method.
  //到接口類(lèi)中尋找
  txAttr = findTransactionAttribute(method.getDeclaringClass());
  if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
   return txAttr;
  }
 }

 return null;
}

getTransactionAttributeSource()獲得的對(duì)象是在ProxyTransactionManagementConfiguration創(chuàng)建bean時(shí)注入的AnnotationTransactionAttributeSource對(duì)象。 AnnotationTransactionAttributeSource中g(shù)etTransactionAttributeSource方法主要邏輯交給了computeTransactionAttribute方法,所以我們直接看computeTransactionAttribute代碼實(shí)現(xiàn)。

computeTransactionAttribute方法執(zhí)行的邏輯是:

  1. 判斷是不是只運(yùn)行公共方法,在AnnotationTransactionAttributeSource構(gòu)造方法中傳入true。若方法不是公共方法,則返回null。
  2. 得到具體的方法,method方法可能是接口方法或者泛型方法。
  3. 查看方法上是否存在事務(wù)
  4. 查看方法所在類(lèi)上是否存在事務(wù)
  5. 查看接口的方法是否存在事務(wù),查看接口上是否存在事務(wù)。

所以如果一個(gè)方法上用了@Transactional,類(lèi)上和接口上也用了,以方法上的為主,其次才是類(lèi),最后才到接口。

獲取TransactionManager,具體代碼執(zhí)行流程如下:

protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) {
 // Do not attempt to lookup tx manager if no tx attributes are set
 if (txAttr == null || this.beanFactory == null) {
  return getTransactionManager();
 }
 String qualifier = txAttr.getQualifier();
 if (StringUtils.hasText(qualifier)) {
  return determineQualifiedTransactionManager(qualifier);
 }
 else if (StringUtils.hasText(this.transactionManagerBeanName)) {
  return determineQualifiedTransactionManager(this.transactionManagerBeanName);
 }
 else {
  //常用的會(huì)走到這里
  PlatformTransactionManager defaultTransactionManager = getTransactionManager();
  if (defaultTransactionManager == null) {
   defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY);
   if (defaultTransactionManager == null) {
    //從beanFactory獲取PlatformTransactionManager類(lèi)型的bean
    defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class);
    this.transactionManagerCache.putIfAbsent(
      DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager);
   }
  }
  return defaultTransactionManager;
 }
}
@Bean
public PlatformTransactionManager txManager() {
 return new DataSourceTransactionManager(dataSource());
}

創(chuàng)建事務(wù)主要兩部分:

  • 獲取事務(wù)狀態(tài)
  • 構(gòu)建事務(wù)信息

獲取事務(wù)狀態(tài)

代碼如下:

@Override
 public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
 //1.獲取事務(wù)
 Object transaction = doGetTransaction();

 // Cache debug flag to avoid repeated checks.
 boolean debugEnabled = logger.isDebugEnabled();

 if (definition == null) {
  // Use defaults if no transaction definition given.
  definition = new DefaultTransactionDefinition();
 }

 //判斷當(dāng)前線程是否存在事務(wù),判斷依據(jù)為當(dāng)前線程記錄連接不為空且連接中的(connectionHolder)中的transactionActive屬性不為空
 if (isExistingTransaction(transaction)) {
  // Existing transaction found -> check propagation behavior to find out how to behave.
  return handleExistingTransaction(definition, transaction, debugEnabled);
 }

 // Check definition settings for new transaction.
 //事務(wù)超時(shí)設(shè)置驗(yàn)證
 if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
  throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());
 }

 // No existing transaction found -> check propagation behavior to find out how to proceed.
 //如果當(dāng)前線程不存在事務(wù),但是@Transactional卻聲明事務(wù)為PROPAGATION_MANDATORY拋出異常
 if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {
  throw new IllegalTransactionStateException(
    "No existing transaction found for transaction marked with propagation 'mandatory'");
 }
 //如果當(dāng)前線程不存在事務(wù),PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED都得創(chuàng)建事務(wù)
 else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||
   definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||
   definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
  //空掛起
  SuspendedResourcesHolder suspendedResources = suspend(null);
  if (debugEnabled) {
   logger.debug("Creating new transaction with name [" + definition.getName() + "]: " + definition);
  }
  try {
   //默認(rèn)返回true
   boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
   //構(gòu)建事務(wù)狀態(tài)
   DefaultTransactionStatus status = newTransactionStatus(
     definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
   //構(gòu)造transaction、包括設(shè)置connectionHolder、隔離級(jí)別、timeout
   //如果是新事務(wù),綁定到當(dāng)前線程
   doBegin(transaction, definition);
   //新事務(wù)同步設(shè)置,針對(duì)當(dāng)前線程
   prepareSynchronization(status, definition);
   return status;
  }
  catch (RuntimeException ex) {
   resume(null, suspendedResources);
   throw ex;
  }
  catch (Error err) {
   resume(null, suspendedResources);
   throw err;
  }
 }
 else {
  // Create "empty" transaction: no actual transaction, but potentially synchronization.
  if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && logger.isWarnEnabled()) {
   logger.warn("Custom isolation level specified but no actual transaction initiated; " +
     "isolation level will effectively be ignored: " + definition);
  }
  //聲明事務(wù)是PROPAGATION_SUPPORTS
  boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
  return prepareTransactionStatus(definition, null, true, newSynchronization, debugEnabled, null);
 }
}

構(gòu)建事務(wù)信息

  1. 獲取事務(wù),創(chuàng)建對(duì)應(yīng)的事務(wù)實(shí)例,這里使用的是DataSourceTransactionManager中的doGetTransaction方法,創(chuàng)建基于JDBC的事務(wù)實(shí)例,如果當(dāng)前線程中存在關(guān)于dataSoruce的連接,那么直接使用。這里有一個(gè)對(duì)保存點(diǎn)的設(shè)置,是否開(kāi)啟允許保存點(diǎn)取決于是否設(shè)置了允許嵌入式事務(wù)。DataSourceTransactionManager默認(rèn)是開(kāi)啟的。
  2. 如果當(dāng)先線程存在事務(wù),則轉(zhuǎn)向嵌套的事務(wù)處理。是否存在事務(wù)在DataSourceTransactionManager的isExistingTransaction方法中
  3. 事務(wù)超時(shí)設(shè)置驗(yàn)證
  4. 事務(wù)PropagationBehavior屬性的設(shè)置驗(yàn)證
  5. 構(gòu)建DefaultTransactionStatus。
  6. 完善transaction,包括設(shè)置connectionHolder、隔離級(jí)別、timeout,如果是新事務(wù),綁定到當(dāng)前線程
  7. 將事務(wù)信息記錄在當(dāng)前線程中

以上就是Spring事務(wù)執(zhí)行流程及如何創(chuàng)建事務(wù)的詳細(xì)內(nèi)容,更多關(guān)于Spring事務(wù)執(zhí)行流程及如何創(chuàng)建的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java 如何調(diào)用Python文件包括傳參

    java 如何調(diào)用Python文件包括傳參

    這篇文章主要介紹了java 調(diào)用Python文件包括傳參的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • 關(guān)于Gateway路由匹配規(guī)則解讀

    關(guān)于Gateway路由匹配規(guī)則解讀

    本文詳細(xì)介紹了SpringCloudGateway的路由匹配規(guī)則,包括基本概念、常用屬性、實(shí)際應(yīng)用以及注意事項(xiàng),路由匹配規(guī)則決定了請(qǐng)求如何被轉(zhuǎn)發(fā)到目標(biāo)服務(wù),是Gateway的核心功能之一,在配置路由時(shí)需要注意順序、性能和安全性
    2025-02-02
  • springboot 接收List 入?yún)⒌膸追N方法

    springboot 接收List 入?yún)⒌膸追N方法

    本文主要介紹了springboot 接收List 入?yún)⒌膸追N方法,本文主要介紹了7種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • Eclipse如何導(dǎo)入Maven項(xiàng)目詳解(新手初學(xué))

    Eclipse如何導(dǎo)入Maven項(xiàng)目詳解(新手初學(xué))

    這篇文章主要介紹了Eclipse如何導(dǎo)入Maven項(xiàng)目詳解(新手初學(xué)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實(shí)例代碼

    Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實(shí)例代碼

    這篇文章主要介紹了Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實(shí)例代碼,涉及使用辛普森積分的例子,這里分享給大家,供需要的朋友參考。
    2017-10-10
  • 解析java.library.path和LD_LIBRARY_PATH的介紹與區(qū)別

    解析java.library.path和LD_LIBRARY_PATH的介紹與區(qū)別

    這篇文章主要介紹了java.library.path和LD_LIBRARY_PATH的介紹與區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • Java利用MessageFormat實(shí)現(xiàn)短信模板的匹配

    Java利用MessageFormat實(shí)現(xiàn)短信模板的匹配

    這篇文章主要介紹了Java利用MessageFormat實(shí)現(xiàn)短信模板的匹配,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • MyBatis高級(jí)映射ResultMap解決屬性問(wèn)題

    MyBatis高級(jí)映射ResultMap解決屬性問(wèn)題

    對(duì)于數(shù)據(jù)庫(kù)中對(duì)表的增刪改查操作,我們知道增刪改都涉及的是單表,而只有查詢操作既可以設(shè)計(jì)到單表操作又可以涉及到多表操作,所以對(duì)于輸入映射parameterType而言是沒(méi)有所謂的高級(jí)映射的,也就是說(shuō)高級(jí)映射只針對(duì)于輸出映射
    2023-02-02
  • SpringBoot文件上傳接口并發(fā)性能調(diào)優(yōu)

    SpringBoot文件上傳接口并發(fā)性能調(diào)優(yōu)

    在一個(gè)項(xiàng)目現(xiàn)場(chǎng),文件上傳接口(文件500K)QPS只有30,這個(gè)并發(fā)性能確實(shí)堪憂,此文記錄出坑過(guò)程,文中通過(guò)代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-06-06
  • Springboot?MBean使用示例解析

    Springboot?MBean使用示例解析

    這篇文章主要為大家介紹了Springboot?MBean使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06

最新評(píng)論

平和县| 安新县| 马山县| 汝阳县| 温宿县| 鱼台县| 大名县| 石屏县| 大港区| 汶上县| 穆棱市| 铜鼓县| 蒲江县| 平凉市| 宝坻区| 广河县| 富宁县| 扶绥县| 惠来县| 米林县| 安西县| 涪陵区| 绵阳市| 阳谷县| 苍南县| 望奎县| 清徐县| 旬阳县| 永城市| 阜新市| 拉孜县| 沅江市| 齐齐哈尔市| 常德市| 天津市| 界首市| 彩票| 富平县| 海南省| 阿合奇县| 蒲城县|