結合ThreadLocal來看spring事務源碼,感受下清泉般的洗滌!


前言

  在我的博客spring事務源碼解析中,提到了一個很關鍵的點:將connection綁定到當前線程來保證這個線程中的數據庫操作用的是同一個connection。但是沒有細致的講到如何綁定,以及為什么這么綁定;另外也沒有講到連接池的相關問題:如何從連接池獲取,如何歸還連接到連接池等等。那么下面就請聽我慢慢道來。

  路漫漫其修遠兮,吾將上下而求索!

  github:https://github.com/youzhibing

  碼雲(gitee):https://gitee.com/youzhibing

ThreadLocal

  講spring事務之前,我們先來看看ThreadLocal,它在spring事務中是占據着比較重要的地位;不管你對ThreadLocal熟悉與否,且都靜下心來聽我唐僧般的念叨。

  先強調一點:ThreadLocal不是用來解決共享變量問題的,它與多線程的並發問題沒有任何關系

  基本介紹

    當使用ThreadLocal維護變量時,ThreadLocal為每個使用該變量的線程提供獨立的變量副本,所以每一個線程都可以獨立地改變自己的副本,而不會影響其它線程所對應的副本,如下例:

public class ThreadLocalTest
{
    ThreadLocal<Long> longLocal = new ThreadLocal<Long>();
    ThreadLocal<String> stringLocal = new ThreadLocal<String>();
    
    public void set()
    {
        longLocal.set(1L);
        stringLocal.set(Thread.currentThread().getName());
    }
    
    public long getLong()
    {
        return longLocal.get();
    }
    
    public String getString()
    {
        return stringLocal.get();
    }
    
    public static void main(String[] args) throws InterruptedException
    {
        final ThreadLocalTest test = new ThreadLocalTest();
        
        test.set();     // 初始化ThreadLocal
        for (int i=0; i<10; i++)
        {
            System.out.println(test.getString() + " : " + test.getLong() + i);
        }
        
        Thread thread1 = new Thread(){
            public void run() {
                test.set();
                for (int i=0; i<10; i++)
                {
                    System.out.println(test.getString() + " : " + test.getLong() + i);
                }
            };
        };
        thread1.start();
        
        Thread thread2 = new Thread(){
            public void run() {
                test.set();
                for (int i=0; i<10; i++)
                {
                    System.out.println(test.getString() + " : " + test.getLong() + i);
                }
            };
        };
        thread2.start();
    }
}

    執行結果如下

    可以看到,各個線程的longLocal值與stringLocal值是相互獨立的,本線程的累加操作不會影響到其他線程的值,真正達到了線程內部隔離的效果。

  源碼解讀

    這里我就不進行ThreadLocal的源碼解析,建議大家去看我參考的博客,個人認為看那兩篇博客就能對ThreadLocal有個很深地認知了。

    做個重復的強調(引用[Java並發包學習七]解密ThreadLocal中的一段話):

Thread與ThreadLocal對象之間的引用關系圖
 
看了ThreadLocal源碼,不知道大家有沒有一個疑惑:為什么像上圖那么設計? 如果給你設計,你會怎么設計?相信大部分人會有這樣的想法,我也是這樣的想法:
  ”每個ThreadLocal類創建一個Map,然后用線程的ID作為Map的key,實例對象作為Map的value,這樣就能達到各個線程的值隔離的效果“
JDK最早期的ThreadLocal就是這樣設計的。(不確定是否是1.3)之后ThreadLocal的設計換了一種方式,也就是目前的方式,那有什么優勢了:
  1、這樣設計之后每個Map的Entry數量變小了:之前是Thread的數量,現在是ThreadLocal的數量,能提高性能,據說性能的提升不是一點兩點(沒有親測)
  2、當Thread銷毀之后對應的ThreadLocalMap也就隨之銷毀了,能減少內存使用量。

    總結下改進后的優點

      1、自動釋放, 當 Thread 對象銷毀后,ThreadLocalMap 對象也隨之銷毀,JVM 及時回收,避免了內存泄漏。如果按我們的想法:定義一個靜態的map,將當前 thread(或 thread 的 ID) 作為key,需要保存的對象作為 value,put 到 map 中;如果任務完成之后,當前線程銷毀了,這個靜態 map 中該線程的信息不會自動回收,如果我們不手動去釋放,這個 map 會隨着時間的積累越來越大,最后出現內存泄漏。而一旦需要進行手動釋放,那很有可能就會有漏網之魚,這就像埋一個定時炸彈,定期爆發,而又不好排查!

      2、性能提升,各線程訪問的 ThreadLocalMap 是各自不同的 ThreadLocalMap,所以不需要同步,速度會快很多;而如果把所有線程要用的對象都放到一個靜態 map 中的話,多線程並發訪問需要進行同步(有興趣的可以去看下 JDK1.3的實現)

Spring事務中的ThreadLocal

  最常見的ThreadLocal使用場景為 用來解決數據庫連接、Session管理等,那么接下來我們就看看spring事務中ThreadLocal的應用

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-jdbc.xml");
DaoImpl daoImpl = (DaoImpl) ac.getBean("daoImpl");
System.out.println(daoImpl.insertUser("yes", 25));

  只要某個類的方法、類或者接口上有事務配置,spring就會對該類的實例生成代理。所以daoImpl是DaoImpl實例的代理實例的引用,而不是DaoImpl的實例(目標實例)的引用;當我們調用目標實例的方法時,實際調用的是代理實例對應的方法,若目標方法沒有被@Transactional(或aop注解,當然這里不涉及aop)修飾,那么代理方法直接反射調用目標方法,若目標方法被@Transactional修飾,那么代理方法會先執行增強(例如判斷當前線程是否存在connection,不存在則新建並綁定到當前線程等等),然后通過反射執行目標方法,最后回到代理方法執行增強(例如,事務回滾或事務提交、connection歸還到連接池等等處理)。這里的綁定connection到當前線程就用到了ThreadLocal,我們來看看源碼

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
    Connection con = null;

    try {
        
        if (txObject.getConnectionHolder() == null ||
                txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
            // 從連接池獲取一個connection
            Connection newCon = this.dataSource.getConnection();
            if (logger.isDebugEnabled()) {
                logger.debug("Acquired Connection [" + newCon + "] for JDBC transaction");
            }
            // 包裝newCon,並賦值到txObject,並標記是新的ConnectionHolder
            txObject.setConnectionHolder(new ConnectionHolder(newCon), true);
        }

        txObject.getConnectionHolder().setSynchronizedWithTransaction(true);
        con = txObject.getConnectionHolder().getConnection();

        Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
        txObject.setPreviousIsolationLevel(previousIsolationLevel);

        // Switch to manual commit if necessary. This is very expensive in some JDBC drivers,
        // so we don't want to do it unnecessarily (for example if we've explicitly
        // configured the connection pool to set it already).
        if (con.getAutoCommit()) {
            txObject.setMustRestoreAutoCommit(true);
            if (logger.isDebugEnabled()) {
                logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
            }
            con.setAutoCommit(false);
        }
        txObject.getConnectionHolder().setTransactionActive(true);

        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getConnectionHolder().setTimeoutInSeconds(timeout);
        }

        // 若是新的ConnectionHolder,則將它綁定到當前線程中
        // Bind the session holder to the thread.
        if (txObject.isNewConnectionHolder()) {
            TransactionSynchronizationManager.bindResource(getDataSource(), txObject.getConnectionHolder());
        }
    }

    catch (Throwable ex) {
        if (txObject.isNewConnectionHolder()) {
            DataSourceUtils.releaseConnection(con, this.dataSource);
            txObject.setConnectionHolder(null, false);
        }
        throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);
    }
}
/**
 * Bind the given resource for the given key to the current thread.
 * @param key the key to bind the value to (usually the resource factory)
 * @param value the value to bind (usually the active resource object)
 * @throws IllegalStateException if there is already a value bound to the thread
 * @see ResourceTransactionManager#getResourceFactory()
 */
public static void bindResource(Object key, Object value) throws IllegalStateException {        //key:通常指資源工廠,也就是connection工廠,value:通常指活動的資源,也就是活動的ConnectionHolder
    
    // 必要時unwrap給定的連接池; 否則按原樣返回給定的連接池。
    Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
    Assert.notNull(value, "Value must not be null");
    Map<Object, Object> map = resources.get();
    // set ThreadLocal Map if none found   如果ThreadLocal Map不存在則新建,並將其設置到resources中
    // private static final ThreadLocal<Map<Object, Object>> resources = new NamedThreadLocal<Map<Object, Object>>("Transactional resources");
  // 這就到了ThreadLocal流程了 if (map == null) { map = new HashMap<Object, Object>(); resources.set(map); } Object oldValue = map.put(actualKey, value); // Transparently suppress a ResourceHolder that was marked as void... if (oldValue instanceof ResourceHolder && ((ResourceHolder) oldValue).isVoid()) { oldValue = null; } if (oldValue != null) { throw new IllegalStateException("Already value [" + oldValue + "] for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]"); } if (logger.isTraceEnabled()) { logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to thread [" + Thread.currentThread().getName() + "]"); } }

總結

  1、ThreadLocal能解決的問題,那肯定不是共享變量(多線程並發)問題,只是看起來有些像並發;像火車票、電影票這樣的真正的共享變量的問題用ThreadLocal是解決不了的,同一時間,同一趟車的同一個座位,你敢用ThreadLocal來解決嗎?

  2、每個Thread維護一個ThreadLocalMap映射表,這個映射表的key是ThreadLocal實例本身,value是真正需要存儲的Object

  3、druid連接池用的是數組來存放的connectionHolder,不是我認為的list,connectionHolder從線程中解綁后,歸還到數組連接池中;connectionHolder是connection的封裝

疑問

  private static final ThreadLocal<Map<Object, Object>> resources = new NamedThreadLocal<Map<Object, Object>>("Transactional resources");

  1、 為什么是ThreadLocal<Map<Object, Object>>,而不是ThreadLocal<ConnectionHolder>

  2、 ThreadLocal<Map<Object, Object>> 中的Map的key是為什么是DataSource

  望知道的朋友賜教下,評論留言或者私信都可以,謝謝!

參考

  Java並發編程:深入剖析ThreadLocal

  正確理解ThreadLocal


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM