REDIS (15) JedisPool的參數testoncreate,testWhileIdle,testonborrow和testonreturn

來源:互聯網
上載者:User

流程

【testonreturn參數】    JedisPool.returnResource(jedis)->pool.returnObject(jedis)->pool.getTestOnReturn()                                                   |                                                             |                                                                     |                                                                                     |   【testOnborrow參數】 JedisPool.getResource()->pool.borrowObject()    |                                                         |              |                                                         \           |                                                           \         |                                                             \       |                                                              _\|   \|/                                                              PooledObjectFactory.validateObject(PooledObject<T>)                                                                  |                                                                  |--->JedisFactory.validateObject(PooledObject<Jedis>)-->ping通,不通銷毀                                                                  |                                                                  |-->ShardedJedisFactory.validateObject(ooledObject<Jedis>)->叢集所有節點ping通,不通銷毀

org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(long borrowMaxWaitMillis) throws Exception

public T borrowObject(long borrowMaxWaitMillis) throws Exception {        assertOpen();        AbandonedConfig ac = this.abandonedConfig;        if (ac != null && ac.getRemoveAbandonedOnBorrow() &&                (getNumIdle() < 2) &&                (getNumActive() > getMaxTotal() - 3) ) {            removeAbandoned(ac);        }        PooledObject<T> p = null;        // Get local copy of current config so it is consistent for entire        // method execution        boolean blockWhenExhausted = getBlockWhenExhausted();        boolean create;        long waitTime = System.currentTimeMillis();        while (p == null) {            create = false;            if (blockWhenExhausted) {                p = idleObjects.pollFirst();                if (p == null) {                    p = create();                    if (p != null) {                        create = true;                    }                }                if (p == null) {                    if (borrowMaxWaitMillis < 0) {                        p = idleObjects.takeFirst();                    } else {                        p = idleObjects.pollFirst(borrowMaxWaitMillis,                                TimeUnit.MILLISECONDS);                    }                }                if (p == null) {                    throw new NoSuchElementException(                            "Timeout waiting for idle object");                }                if (!p.allocate()) {                    p = null;                }            } else {                p = idleObjects.pollFirst();                if (p == null) {                    p = create();                    if (p != null) {                        create = true;                    }                }                if (p == null) {                    throw new NoSuchElementException("Pool exhausted");                }                if (!p.allocate()) {                    p = null;                }            }            if (p != null) {                try {                    factory.activateObject(p);                } catch (Exception e) {                    try {                        destroy(p);                    } catch (Exception e1) {                        // Ignore - activation failure is more important                    }                    p = null;                    if (create) {                        NoSuchElementException nsee = new NoSuchElementException(                                "Unable to activate object");                        nsee.initCause(e);                        throw nsee;                    }                }                if (p != null && (getTestOnBorrow() || create && getTestOnCreate())) {                    boolean validate = false;                    Throwable validationThrowable = null;                    try {                        validate = factory.validateObject(p);                    } catch (Throwable t) {                        PoolUtils.checkRethrow(t);                        validationThrowable = t;                    }                    if (!validate) {                        try {                            destroy(p);                            destroyedByBorrowValidationCount.incrementAndGet();                        } catch (Exception e) {                            // Ignore - validation failure is more important                        }                        p = null;                        if (create) {                            NoSuchElementException nsee = new NoSuchElementException(                                    "Unable to validate object");                            nsee.initCause(validationThrowable);                            throw nsee;                        }                    }                }            }        }        updateStatsBorrow(p, System.currentTimeMillis() - waitTime);        return p.getObject();    }

···

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.