欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jedis的return行為源碼解析

 更新時間:2023年09月22日 10:16:56   作者:codecraft  
這篇文章主要為大家介紹了jedis的return行為源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

本文主要研究一下jedis的return行為

spring-data-redis

RedisTemplate

org/springframework/data/redis/core/RedisTemplate.java

@Nullable
    public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) {
        Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");
        Assert.notNull(action, "Callback object must not be null");
        RedisConnectionFactory factory = getRequiredConnectionFactory();
        RedisConnection conn = RedisConnectionUtils.getConnection(factory, enableTransactionSupport);
        try {
            boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);
            RedisConnection connToUse = preProcessConnection(conn, existingConnection);
            boolean pipelineStatus = connToUse.isPipelined();
            if (pipeline && !pipelineStatus) {
                connToUse.openPipeline();
            }
            RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
            T result = action.doInRedis(connToExpose);
            // close pipeline
            if (pipeline && !pipelineStatus) {
                connToUse.closePipeline();
            }
            return postProcessResult(result, connToUse, existingConnection);
        } finally {
            RedisConnectionUtils.releaseConnection(conn, factory, enableTransactionSupport);
        }
    }
RedisTemplate的execute方法先通過RedisConnectionUtils.getConnection獲取連接,最后通過RedisConnectionUtils.releaseConnection來歸還連接

RedisConnectionUtils

org/springframework/data/redis/core/RedisConnectionUtils.java

public static void releaseConnection(@Nullable RedisConnection conn, RedisConnectionFactory factory,
            boolean transactionSupport) {
        releaseConnection(conn, factory);
    }
    public static void releaseConnection(@Nullable RedisConnection conn, RedisConnectionFactory factory) {
        if (conn == null) {
            return;
        }
        RedisConnectionHolder conHolder = (RedisConnectionHolder) TransactionSynchronizationManager.getResource(factory);
        if (conHolder != null) {
            if (conHolder.isTransactionActive()) {
                if (connectionEquals(conHolder, conn)) {
                    if (log.isDebugEnabled()) {
                        log.debug("RedisConnection will be closed when transaction finished.");
                    }
                    // It's the transactional Connection: Don't close it.
                    conHolder.released();
                }
                return;
            }
            // release transactional/read-only and non-transactional/non-bound connections.
            // transactional connections for read-only transactions get no synchronizer registered
            unbindConnection(factory);
            return;
        }
        doCloseConnection(conn);
    }
    private static void doCloseConnection(@Nullable RedisConnection connection) {
        if (connection == null) {
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("Closing Redis Connection.");
        }
        try {
            connection.close();
        } catch (DataAccessException ex) {
            log.debug("Could not close Redis Connection", ex);
        } catch (Throwable ex) {
            log.debug("Unexpected exception on closing Redis Connection", ex);
        }
    }
releaseConnection方法主要是處理了事務相關的操作,最后執(zhí)行doCloseConnection,它最后執(zhí)行的是connection.close()

connection.close()

org/springframework/data/redis/connection/jedis/JedisConnection.java

@Override
    public void close() throws DataAccessException {
        super.close();
        JedisSubscription subscription = this.subscription;
        try {
            if (subscription != null) {
                subscription.close();
            }
        } catch (Exception ex) {
            LOGGER.debug("Cannot terminate subscription", ex);
        } finally {
            this.subscription = null;
        }
        // return the connection to the pool
        if (pool != null) {
            jedis.close();
            return;
        }
        // else close the connection normally (doing the try/catch dance)
        try {
            jedis.quit();
        } catch (Exception ex) {
            LOGGER.debug("Failed to QUIT during close", ex);
        }
        try {
            jedis.disconnect();
        } catch (Exception ex) {
            LOGGER.debug("Failed to disconnect during close", ex);
        }
    }
connection的close方法針對使用連接池的會執(zhí)行jedis.close,否則執(zhí)行jedis.quit

jedis.close()

redis/clients/jedis/Jedis.java

@Override
  public void close() {
    if (dataSource != null) {
      JedisPoolAbstract pool = this.dataSource;
      this.dataSource = null;
      if (isBroken()) {
        pool.returnBrokenResource(this);
      } else {
        pool.returnResource(this);
      }
    } else {
      super.close();
    }
  }
jedis的close方法會先判斷isBroken(取的redis.clients.jedis.Connection.broken屬性),如果是則執(zhí)行returnBrokenResource,否則執(zhí)行returnResource

pool

redis/clients/jedis/util/Pool.java

public void returnBrokenResource(final T resource) {
    if (resource != null) {
      returnBrokenResourceObject(resource);
    }
  }
  public void returnResource(final T resource) {
    if (resource != null) {
      returnResourceObject(resource);
    }
  }
  protected void returnBrokenResourceObject(final T resource) {
    try {
      internalPool.invalidateObject(resource);
    } catch (Exception e) {
      throw new JedisException("Could not return the broken resource to the pool", e);
    }
  }
  protected void returnResourceObject(final T resource) {
    try {
      internalPool.returnObject(resource);
    } catch (RuntimeException e) {
      throw new JedisException("Could not return the resource to the pool", e);
    }
  }
returnBrokenResource執(zhí)行的是internalPool.invalidateObject(resource),而returnResourceObject執(zhí)行的是internalPool.returnObject(resource)

invalidateObject

org/apache/commons/pool2/impl/GenericObjectPool.java

public void invalidateObject(final T obj, final DestroyMode destroyMode) throws Exception {
        final PooledObject<T> p = getPooledObject(obj);
        if (p == null) {
            if (isAbandonedConfig()) {
                return;
            }
            throw new IllegalStateException(
                    "Invalidated object not currently part of this pool");
        }
        synchronized (p) {
            if (p.getState() != PooledObjectState.INVALID) {
                destroy(p, destroyMode);
            }
        }
        ensureIdle(1, false);
    }
    private void destroy(final PooledObject<T> toDestroy, final DestroyMode destroyMode) throws Exception {
        toDestroy.invalidate();
        idleObjects.remove(toDestroy);
        allObjects.remove(new IdentityWrapper<>(toDestroy.getObject()));
        try {
            factory.destroyObject(toDestroy, destroyMode);
        } finally {
            destroyedCount.incrementAndGet();
            createCount.decrementAndGet();
        }
    }
invalidateObject方法執(zhí)行的是destroy方法,該方法會回調(diào)factory.destroyObject

destroyObject

redis/clients/jedis/JedisFactory.java

public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception {
    final BinaryJedis jedis = pooledJedis.getObject();
    if (jedis.isConnected()) {
      try {
        // need a proper test, probably with mock
        if (!jedis.isBroken()) {
          jedis.quit();
        }
      } catch (RuntimeException e) {
        logger.warn("Error while QUIT", e);
      }
      try {
        jedis.close();
      } catch (RuntimeException e) {
        logger.warn("Error while close", e);
      }
    }
  }

destroyObject方法則執(zhí)行jedis.close()關閉client連接

returnObject

org/apache/commons/pool2/impl/GenericObjectPool.java

public void returnObject(final T obj) {
        final PooledObject<T> p = getPooledObject(obj);
        if (p == null) {
            if (!isAbandonedConfig()) {
                throw new IllegalStateException(
                        "Returned object not currently part of this pool");
            }
            return; // Object was abandoned and removed
        }
        markReturningState(p);
        final Duration activeTime = p.getActiveDuration();
        if (getTestOnReturn() && !factory.validateObject(p)) {
            try {
                destroy(p, DestroyMode.NORMAL);
            } catch (final Exception e) {
                swallowException(e);
            }
            try {
                ensureIdle(1, false);
            } catch (final Exception e) {
                swallowException(e);
            }
            updateStatsReturn(activeTime);
            return;
        }
        try {
            factory.passivateObject(p);
        } catch (final Exception e1) {
            swallowException(e1);
            try {
                destroy(p, DestroyMode.NORMAL);
            } catch (final Exception e) {
                swallowException(e);
            }
            try {
                ensureIdle(1, false);
            } catch (final Exception e) {
                swallowException(e);
            }
            updateStatsReturn(activeTime);
            return;
        }
        if (!p.deallocate()) {
            throw new IllegalStateException(
                    "Object has already been returned to this pool or is invalid");
        }
        final int maxIdleSave = getMaxIdle();
        if (isClosed() || maxIdleSave > -1 && maxIdleSave <= idleObjects.size()) {
            try {
                destroy(p, DestroyMode.NORMAL);
            } catch (final Exception e) {
                swallowException(e);
            }
            try {
                ensureIdle(1, false);
            } catch (final Exception e) {
                swallowException(e);
            }
        } else {
            if (getLifo()) {
                idleObjects.addFirst(p);
            } else {
                idleObjects.addLast(p);
            }
            if (isClosed()) {
                // Pool closed while object was being added to idle objects.
                // Make sure the returned object is destroyed rather than left
                // in the idle object pool (which would effectively be a leak)
                clear();
            }
        }
        updateStatsReturn(activeTime);
    }

returnObject針對testOnReturn的會執(zhí)行validateObject方法,之后執(zhí)行factory.passivateObject(p),最后根據(jù)maxIdle的參數(shù)來判斷,超出的則執(zhí)行destroy,否則根據(jù)是否Lifo放回到連接池(idleObjects)中

小結

spring-data-redis的return主要是執(zhí)行connection的close方法,對應到jedis就是jedis.close(),它會先判斷isBroken(取的redis.clients.jedis.Connection.broken屬性),如果是則執(zhí)行returnBrokenResource,否則執(zhí)行returnResource。

  • returnBrokenResource執(zhí)行的是internalPool.invalidateObject(resource),invalidateObject方法執(zhí)行的是destroy方法,該方法會回調(diào)factory.destroyObject方法,即執(zhí)行jedis.close()關閉client連接
  • returnObject針對testOnReturn的會執(zhí)行validateObject方法,之后執(zhí)行factory.passivateObject(p),最后根據(jù)maxIdle的參數(shù)來判斷,超出的則執(zhí)行destroy,否則根據(jù)是否Lifo放回到連接池(idleObjects)中
  • 也就說假設獲取連接之后,執(zhí)行的時候redis掛了,redis.clients.jedis.Connection會標記broken為true,同時拋出JedisConnectionException;而RedisTemplate是在finally中進行releaseConnection,因而歸還的時候會觸發(fā)returnBrokenResource從而關閉壞掉的連接,間接實現(xiàn)testOnReturn的效果
  • 如果在獲取連接的時候,redis掛了,但是連接池仍然有連接,若沒有testOnBorrow則返回然后使用,但是使用的時候會報錯,即redis.clients.jedis.Connection會標記broken為true,同時拋出JedisConnectionException,歸還的時候直接銷毀;若有testOnBorrow則validate的時候能驗證出來連接有問題,則會執(zhí)行destory然后繼續(xù)循環(huán)獲取連接池的連接,直到連接池連接沒有了;若獲取連接的時候連接池沒有空閑連接了,則走create的邏輯,這個時候create直接拋出redis.clients.jedis.exceptions.JedisConnectionException: Failed to create socket.

以上就是jedis的return行為源碼解析的詳細內(nèi)容,更多關于jedis return行為的資料請關注腳本之家其它相關文章!

相關文章

  • java利用遞歸算法實現(xiàn)對文件夾的刪除功能

    java利用遞歸算法實現(xiàn)對文件夾的刪除功能

    這篇文章主要介紹了java利用遞歸算法實現(xiàn)對文件夾的刪除功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • java獲取中文拼音首字母工具類定義與用法實例

    java獲取中文拼音首字母工具類定義與用法實例

    這篇文章主要介紹了java獲取中文拼音首字母工具類定義與用法,結合實例形式分析了java獲取中文拼音首字母工具類的具體定義、使用方法及相關操作注意事項,需要的朋友可以參考下
    2019-10-10
  • 詳解Java中JSON數(shù)據(jù)的生成與解析

    詳解Java中JSON數(shù)據(jù)的生成與解析

    今天給大家?guī)淼氖顷P于Java的相關知識,文章圍繞著Java中JSON數(shù)據(jù)的生成與解析展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • java 數(shù)值類型分秒時間格式化的實例代碼

    java 數(shù)值類型分秒時間格式化的實例代碼

    這篇文章主要介紹了java 數(shù)值類型分秒時間格式化的實例代碼的相關資料,將秒或分鐘的值轉換為xx天xx小時xx分鐘xx秒 如果 “xx” 為0 自動缺省,需要的朋友可以參考下
    2017-07-07
  • 利用github搭建個人maven倉庫的方法步驟

    利用github搭建個人maven倉庫的方法步驟

    這篇文章主要介紹了利用github搭建個人maven倉庫的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 淺析SpringBoot自動化配置原理實現(xiàn)

    淺析SpringBoot自動化配置原理實現(xiàn)

    這篇文章主要介紹了淺析SpringBoot自動化配置原理實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • selenium高效應對Web頁面元素刷新的實例講解

    selenium高效應對Web頁面元素刷新的實例講解

    今天小編就為大家分享一篇selenium高效應對Web頁面元素刷新的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 淺析Java中的訪問控制權限

    淺析Java中的訪問控制權限

    這篇文章主要介紹了淺析Java中的訪問控制權限,在Java中,提供了四種訪問權限控制,分別是默認訪問權限、public、private以及protected,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Springboot打包成jar發(fā)布的操作方法

    Springboot打包成jar發(fā)布的操作方法

    打包的方式有打包成jar包或者打包成war包發(fā)布,區(qū)別在于jar包內(nèi)置了tomcat、netty等服務器,更改只需要修改pom.xml的坐標即可,war不內(nèi)置服務器,需要上傳到服務器tomcat解壓后運行,本文分析Springboot打包成jar發(fā)布,感興趣的朋友一起看看吧
    2023-02-02
  • Java之如何獲取泛型參數(shù)

    Java之如何獲取泛型參數(shù)

    在Java開發(fā)中,獲取泛型參數(shù)一般有兩種方法:第一種是通過JDK自帶的API,主要利用反射機制來獲取類的泛型信息;第二種方法是借助Spring框架提供的GenericTypeResolver工具類,這種方式更加簡便,這兩種方法都能有效地幫助開發(fā)者在運行時獲取到泛型參數(shù)
    2024-09-09

最新評論