成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

詳解Spring多線程下如何保證事務的一致性

開發 前端
我們先來大概的了解下Spring事務的工作原理,核心技術是通過AOP實現,將獲取的Connection對象綁定到當前線程上下文中(ThreadLocal)。

環境:Spring5.3.23

1. 事務原理

首先,我們先來大概的了解下Spring事務的工作原理,核心技術是通過AOP實現,將獲取的Connection對象綁定到當前線程上下文中(ThreadLocal)。

事務核心攔截器TransactionInterceptor對象,如下(以下只會列出核心代碼):

public class TransactionInterceptor {
  public Object invoke(MethodInvocation invocation) {
    // 該方法調用為核心方法,該方法在父類中
    return invokeWithinTransaction(...) ;
  }
}

父類TransactionAspectSupport

public abstract class TransactionAspectSupport {
  protected Object invokeWithinTransaction(...) {
    // 1.1.創建事務對象
    TransactionInfo txInfo = createTransactionIfNecessary(ptm, txAttr, joinpointIdentification);
    try {
        // 調用下一個攔截器或者是目標方法
      retVal = invocation.proceedWithInvocation();
    }
    catch (Throwable ex) {
      // 1.2.回滾事務
      completeTransactionAfterThrowing(txInfo, ex);
      throw ex;
    } finally {
      // 重置ThreadLocal中的TransactionInfo對象
      cleanupTransactionInfo(txInfo);
    }
    // 1.3.提交或者回滾事務
    commitTransactionAfterReturning(txInfo);
    return retVal;
  }  
}

上面代碼列出了主要的事務執行流程及動作,我們主要是關心數據庫連接對象Connection在當前線程中是如何使用的。

創建事務對象

protected TransactionInfo createTransactionIfNecessary(
    @Nullable PlatformTransactionManager tm,
    @Nullable TransactionAttribute txAttr, 
    final String joinpointIdentification) {
  TransactionStatus status = null;
  if (txAttr != null) {
    if (tm != null) {
      // 創建事務狀態對象
      status = tm.getTransaction(txAttr);
    }
  }
  // 將事務狀態對象包裝到TransactionInfo中,然后將這個對象綁定到當前線程中
  return prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
}

創建事務狀態對象

public abstract class AbstractPlatformTransactionManager {
  public final TransactionStatus getTransaction(...) {
    if (isExistingTransaction(transaction)) {
      // Existing transaction found -> check propagation behavior to find out how to behave.
      return handleExistingTransaction(def, transaction, debugEnabled);
    }


    // 如果超時時間 < -1則拋出異常
    if (def.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
      throw new InvalidTimeoutException("Invalid transaction timeout", def.getTimeout());
    }


    // 當前不存在事務,則拋出異常
    if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {
      throw new IllegalTransactionStateException(
          "No existing transaction found for transaction marked with propagation 'mandatory'");
    }
    // 其它的傳播特性,開啟事務功能
    else if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||
        def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||
        def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
      try {
        // 開始事務
        return startTransaction(def, transaction, debugEnabled, suspendedResources);
      }
    }
  }
}

開始事務

private TransactionStatus startTransaction(
    TransactionDefinition definition, 
    Object transaction,
    boolean debugEnabled, 
    @Nullable SuspendedResourcesHolder suspendedResources) {


  boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
  DefaultTransactionStatus status = newTransactionStatus(
      definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
  //     
  doBegin(transaction, definition);
  prepareSynchronization(status, definition);
  return status;
}

創建Connection對象,并綁定到當前線程

public class DataSourceTransactionManager {
  protected void doBegin(
      Object transaction, 
      TransactionDefinition definition) {
    DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
    Connection con = null;
    try {
      if (!txObject.hasConnectionHolder() ||
          txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
        // 獲取數據庫連接對象  
        Connection newCon = obtainDataSource().getConnection();
        txObject.setConnectionHolder(new ConnectionHolder(newCon), true);
      }
      // 將連接對象綁定到當前的線程
      if (txObject.isNewConnectionHolder()) {
        TransactionSynchronizationManager.bindResource(obtainDataSource(), txObject.getConnectionHolder());
      }
    }
  }
}

到此,已經清楚了當開始一個新的事務時,Spring會將獲取的Connection綁定到當前的Thread中。

當我們使用通過JdbcTemplate操作數據庫時,如下:

public class JdbcTemplate {
  // 核心執行方法
  private <T> T execute(...) {
    // 獲取數據庫連接對象
    Connection con = DataSourceUtils.getConnection(obtainDataSource());
  }
}

DataSourceUtils

public abstract class DataSourceUtils {
  public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
    try {
      return doGetConnection(dataSource) ;
    }
  }
  public static Connection doGetConnection(DataSource dataSource) throws SQLException {
    // 通過TransactionSynchronizationManager從當前線程上下文中獲取連接對象
    // 在上面我們也是通過這個對象將連接對象綁定到當前的Thread中
    ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
    if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
      conHolder.requested() ;
      if (!conHolder.hasConnection()) {
        conHolder.setConnection(fetchConnection(dataSource)) ;
      }
      return conHolder.getConnection() ;
    }
  }
}

原理相信你應該非常清楚了,每個線程都會綁定自己的Connection。那在多線程下每個線程都使用的是自己的Connection對象,所以要想保證事務的一致性,單靠傳統的方式一個@Transaction是肯定無法解決的,接下來我們就來實現一個多線程下的事務一致性的處理。

2.多線程事務

多線程下要實現事務的一致性,我們需要借助JUC下的相關類來實現。

這里直接給出代碼示例:

static class PersonService {
  @Resource
  private JdbcTemplate jdbcTemplate;
  @Resource
  private DataSource dataSource ; 


  @Transactional
  public void save() throws Exception {
    CountDownLatch cdl = new CountDownLatch(2) ;
    AtomicBoolean txRollback = new AtomicBoolean(false) ;
    CompletableFuture.runAsync(() -> {
      Person person = new Person();
      person.setAge(1);
      person.setName("張三");
      transactionTemplate.execute(status -> {
        int result = 0 ;
        try {
          result = jdbcTemplate.update("insert into t_person (age, name) values (?, ?)", person.getAge(), person.getName()) ;
          // TODO
          // System.out.println(1 / 0) ;
        } catch (Exception e) {
            // 當發生異常后將狀態該為true
          txRollback.set(true) ;
        }
        try {
            // 計數減一
          cdl.countDown() ;
          // 繼續等待其它線程結束
          cdl.await() ;
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
          // 如果回滾狀態為true說明有線程發生了異常,需要事務回滾
        if (txRollback.get()) {
          // 標記當前事務回滾
          status.setRollbackOnly() ;
        }
        System.out.printf("%s Insert Operator Result: %d 次%n", Thread.currentThread().getName(), result);
        return result ;
      }) ;
      }) ;
    transactionTemplate.execute(status -> {
      Person person = new Person();
      person.setAge(2);
      person.setName("李四");
      int result = 0 ;
      try {
        result = jdbcTemplate.update("insert into t_person (age, name) values (?, ?)", person.getAge(), person.getName()) ;
        // TODO
        TimeUnit.SECONDS.sleep(3) ;
      } catch (Exception e) {
        txRollback.set(true) ; 
      }
      try {
        cdl.countDown() ;
        cdl.await() ;
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (txRollback.get()) {
        // 回滾
        status.setRollbackOnly() ;
      }
      System.out.printf("%s Insert Operator Result: %d 次%n", Thread.currentThread().getName(), result);
      return result ;
    }) ;
    cdl.await() ;
    System.err.println("Operator Complete...") ;
  }
}

以上就是借助JUC來實現多線程下的事務一致性問題。

其實如果你真的理解了事務的原理,其實這里還有更加簡單的實現方式,大家可以先思考,咱們下期再說這種簡單的實現方法。

完畢!!!

責任編輯:武曉燕 來源: Spring全家桶實戰案例源碼
相關推薦

2021-03-04 06:49:53

RocketMQ事務

2022-10-19 12:22:53

并發扣款一致性

2020-01-02 09:06:23

微服務數據框架

2025-02-10 03:00:00

2022-08-29 08:38:00

事務一致性

2020-08-05 08:46:10

NFS網絡文件系統

2025-03-27 08:20:54

2019-08-30 12:46:10

并發扣款查詢SQL

2024-12-26 15:01:29

2024-01-10 08:01:55

高并發場景悲觀鎖

2023-09-07 08:11:24

Redis管道機制

2020-04-01 15:50:17

TiDBMySQL數據庫

2020-06-01 22:09:48

緩存緩存同步緩存誤用

2023-05-26 07:34:50

RedisMySQL緩存

2024-08-20 16:13:52

2021-12-14 07:15:57

MySQLRedis數據

2021-07-21 15:50:42

Serverless 業務部署

2022-03-29 10:39:10

緩存數據庫數據

2024-10-16 09:53:07

2024-10-28 12:41:25

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩精品1区2区3区 爱爱综合网 | 日韩欧美亚洲 | 精品久久久久久久久久 | 日韩精品 电影一区 亚洲 | 一级黄色毛片子 | 一色一黄视频 | 午夜精品一区二区三区在线播放 | 欧美综合一区二区三区 | 成人久久18免费网站 | 99精品国产一区二区三区 | 日韩一区二区在线免费观看 | 精品欧美一区二区三区久久久 | 日本视频中文字幕 | 精品国产91亚洲一区二区三区www | 亚洲三区视频 | 久久久久久久久久久一区二区 | 国产亚洲精品精品国产亚洲综合 | 一a一片一级一片啪啪 | 久久久国产一区二区三区四区小说 | 日韩一区二区免费视频 | 岛国毛片在线观看 | 国产真实精品久久二三区 | 国产精品久久久久久久久久久新郎 | 久久综合久久综合久久综合 | 色久伊人 | 国产精品一区二区av | 欧美极品在线 | 久草视频在线播放 | 精品国产一区二区三区日日嗨 | 韩国欧洲一级毛片 | 91精品国产一区二区三区 | 中文字幕精品视频 | 综合色影院 | 欧美二区在线 | 在线日韩中文字幕 | 亚洲精品九九 | 国产中的精品av涩差av | 国产免费一区二区三区 | 一a一片一级一片啪啪 | 色综合色综合色综合 | 久久久久久国产精品免费 |