问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

如何重现could not return the resource to the pool

发布网友 发布时间:2022-05-29 04:11

我来回答

2个回答

热心网友 时间:2024-08-18 22:51

1,JedisPool的使用
<!-- 连接池的配置信息 -->
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 说明一个pool可以有多少个Jedis实例 -->
<property name="maxActive" value="10" />
<!-- 最大Idle-->
<property name="maxIdle" value="5" />
<!-- 最小Idle -->
<property name="minIdle" value="1" />
<!-- 获得一个jedis实例的时候是否检查连接可用性(ping()) -->
<property name="testOnBorrow" value="true" />
<!-- return 一个jedis实例给pool时,是否检查连接可用性(ping()) -->
<property name="testOnReturn" value="true" />
<!-- idle状态监测用异步线程evict进行检查, -->
<property name="testWhileIdle" value="true" />
<!-- 一次最多evict的pool里的jedis实例个数 -->
<property name="numTestsPerEvictionRun" value="10" />
<!-- test idle 线程的时间间隔 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!--最大等待wait时间-->
<property name="maxWait" value="3000" />
<property name="whenExhaustedAction" value="" />
//WHEN_EXHAUSTED_FAIL = 0; 直接抛出异常throw new NoSuchElementException("Pool exhausted");
//WHEN_EXHAUSTED_BLOCK = 1;borrowObject()将会阻塞,直到有可用新的或者空闲的object为止,或者如果配置了maxWait,
//如果请求阻塞超时,将抛出NoSuchElementException.如果maxWait为负数,请求将会无限制的阻
//塞下去,默认配置。
//WHEN_EXHAUSTED_GROW = 2;borrowObject()将会继续创建新的对象,并返回,因此,pool维护的对像数将超出maxActive;
//
</bean>
public String set(String key, String value) {
Jedis jedis = null;
boolean success = true;
try {
jedis = this.pool.getResource();
return jedis.set(key, value);
}catch (JedisException e) {
success = false;
if(jedis != null){
pool.returnBrokenResource(jedis);
}
throw e;
}finally{
if(success && jedis != null){
this.pool.returnResource(jedis);
}
}
}

获取Jedis
pool.getResource();

这个可以直接看Pool的getResource方法,
最终还是GenericObjectPool的borrowObject()方法借用对象
@SuppressWarnings("unchecked")
public T getResource() {
try {
return (T) internalPool.borrowObject();
} catch (Exception e) {
throw new JedisConnectionException(
"Could not get a resource from the pool", e);
}
}

用完归还,调用的是GenericObjectPool的returnObject()方法
pool.returnResource(jedis)
//JedisPool.java
public void returnResource(final BinaryJedis resource) {
returnResourceObject(resource);
}
//Pool.java
public void returnResourceObject(final Object resource) {
try {
internalPool.returnObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
}

出错,调用的是GenericObjectPool的invalidateObject()方法
最后在JedisFactory的destroyObject()中调用jedis.quit()请求Server关闭连接
pool.returnBrokenResource(jedis)
//JedisPool.java
public void returnBrokenResource(final BinaryJedis resource) {
returnBrokenResourceObject(resource);
}
//Pool.java
protected void returnBrokenResourceObject(final Object resource) {
try {
//失效
internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
}
//GenericObjectPool
public void invalidateObject(Object obj)
throws Exception
{
try
{
if (this._factory != null)
this._factory.destroyObject(obj);
}
finally {
synchronized (this) {
this._numActive -= 1;
allocate();
}
}
}
//JedisFactory
public void destroyObject(final Object obj) throws Exception {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
if (jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {

}
}
}
}

JedisPool源代码
package redis.clients.jedis;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool.Config;

import redis.clients.util.Pool;

public class JedisPool extends Pool<Jedis> {

public JedisPool(final Config poolConfig, final String host) {
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(String host, int port) {
this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final String host) {
this(host, Protocol.DEFAULT_PORT);
}

public JedisPool(final Config poolConfig, final String host, int port,
int timeout, final String password) {
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, final int port) {
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {
this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
final int database) {
super(poolConfig, new JedisFactory(host, port, timeout, password, database));
}

public void returnBrokenResource(final BinaryJedis resource) {
returnBrokenResourceObject(resource);
}

public void returnResource(final BinaryJedis resource) {
returnResourceObject(resource);
}

/**
* PoolableObjectFactory custom impl.
*/
private static class JedisFactory extends BasePoolableObjectFactory {
private final String host;
private final int port;
private final int timeout;
private final String password;
private final int database;

public JedisFactory(final String host, final int port,
final int timeout, final String password, final int database) {
super();
this.host = host;
this.port = port;
this.timeout = timeout;
this.password = password;
this.database = database;
}

public Object makeObject() throws Exception {
final Jedis jedis = new Jedis(this.host, this.port, this.timeout);

jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
if( database != 0 ) {
jedis.select(database);
}

return jedis;
}

public void destroyObject(final Object obj) throws Exception {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
if (jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {

}
}
}
}

public boolean validateObject(final Object obj) {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
try {
return jedis.isConnected();/* && jedis.ping().equals("PONG");*/
} catch (final Exception e) {
return false;
}
} else {
return false;

热心网友 时间:2024-08-18 22:51

1,JedisPool的使用
<!-- 连接池的配置信息 -->
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 说明一个pool可以有多少个Jedis实例 -->
<property name="maxActive" value="10" />
<!-- 最大Idle-->
<property name="maxIdle" value="5" />
<!-- 最小Idle -->
<property name="minIdle" value="1" />
<!-- 获得一个jedis实例的时候是否检查连接可用性(ping()) -->
<property name="testOnBorrow" value="true" />
<!-- return 一个jedis实例给pool时,是否检查连接可用性(ping()) -->
<property name="testOnReturn" value="true" />
<!-- idle状态监测用异步线程evict进行检查, -->
<property name="testWhileIdle" value="true" />
<!-- 一次最多evict的pool里的jedis实例个数 -->
<property name="numTestsPerEvictionRun" value="10" />
<!-- test idle 线程的时间间隔 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!--最大等待wait时间-->
<property name="maxWait" value="3000" />
<property name="whenExhaustedAction" value="" />
//WHEN_EXHAUSTED_FAIL = 0; 直接抛出异常throw new NoSuchElementException("Pool exhausted");
//WHEN_EXHAUSTED_BLOCK = 1;borrowObject()将会阻塞,直到有可用新的或者空闲的object为止,或者如果配置了maxWait,
//如果请求阻塞超时,将抛出NoSuchElementException.如果maxWait为负数,请求将会无限制的阻
//塞下去,默认配置。
//WHEN_EXHAUSTED_GROW = 2;borrowObject()将会继续创建新的对象,并返回,因此,pool维护的对像数将超出maxActive;
//
</bean>
public String set(String key, String value) {
Jedis jedis = null;
boolean success = true;
try {
jedis = this.pool.getResource();
return jedis.set(key, value);
}catch (JedisException e) {
success = false;
if(jedis != null){
pool.returnBrokenResource(jedis);
}
throw e;
}finally{
if(success && jedis != null){
this.pool.returnResource(jedis);
}
}
}

获取Jedis
pool.getResource();

这个可以直接看Pool的getResource方法,
最终还是GenericObjectPool的borrowObject()方法借用对象
@SuppressWarnings("unchecked")
public T getResource() {
try {
return (T) internalPool.borrowObject();
} catch (Exception e) {
throw new JedisConnectionException(
"Could not get a resource from the pool", e);
}
}

用完归还,调用的是GenericObjectPool的returnObject()方法
pool.returnResource(jedis)
//JedisPool.java
public void returnResource(final BinaryJedis resource) {
returnResourceObject(resource);
}
//Pool.java
public void returnResourceObject(final Object resource) {
try {
internalPool.returnObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
}

出错,调用的是GenericObjectPool的invalidateObject()方法
最后在JedisFactory的destroyObject()中调用jedis.quit()请求Server关闭连接
pool.returnBrokenResource(jedis)
//JedisPool.java
public void returnBrokenResource(final BinaryJedis resource) {
returnBrokenResourceObject(resource);
}
//Pool.java
protected void returnBrokenResourceObject(final Object resource) {
try {
//失效
internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
}
//GenericObjectPool
public void invalidateObject(Object obj)
throws Exception
{
try
{
if (this._factory != null)
this._factory.destroyObject(obj);
}
finally {
synchronized (this) {
this._numActive -= 1;
allocate();
}
}
}
//JedisFactory
public void destroyObject(final Object obj) throws Exception {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
if (jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {

}
}
}
}

JedisPool源代码
package redis.clients.jedis;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool.Config;

import redis.clients.util.Pool;

public class JedisPool extends Pool<Jedis> {

public JedisPool(final Config poolConfig, final String host) {
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(String host, int port) {
this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final String host) {
this(host, Protocol.DEFAULT_PORT);
}

public JedisPool(final Config poolConfig, final String host, int port,
int timeout, final String password) {
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, final int port) {
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {
this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
final int database) {
super(poolConfig, new JedisFactory(host, port, timeout, password, database));
}

public void returnBrokenResource(final BinaryJedis resource) {
returnBrokenResourceObject(resource);
}

public void returnResource(final BinaryJedis resource) {
returnResourceObject(resource);
}

/**
* PoolableObjectFactory custom impl.
*/
private static class JedisFactory extends BasePoolableObjectFactory {
private final String host;
private final int port;
private final int timeout;
private final String password;
private final int database;

public JedisFactory(final String host, final int port,
final int timeout, final String password, final int database) {
super();
this.host = host;
this.port = port;
this.timeout = timeout;
this.password = password;
this.database = database;
}

public Object makeObject() throws Exception {
final Jedis jedis = new Jedis(this.host, this.port, this.timeout);

jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
if( database != 0 ) {
jedis.select(database);
}

return jedis;
}

public void destroyObject(final Object obj) throws Exception {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
if (jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {

}
}
}
}

public boolean validateObject(final Object obj) {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
try {
return jedis.isConnected();/* && jedis.ping().equals("PONG");*/
} catch (final Exception e) {
return false;
}
} else {
return false;
}
}
}
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
此去经年两相忘,是出自哪里? 李续宾在安徽“三河战败”殒命主要原因? 三河镇之战的结果 钉钉系统是什么 企业用车、差旅等费用去向不明,财务如何通过滴滴企业版实现企业出行费用... 困扰间采的4大差旅管理难题,该怎么做? 123soul的I'm yours的MP3 清华管理学系列英文版教材:管理学内容简介 入党日期填哪个 女人停经后纯无“性"生活,该咋办 春喧在那句诗中出现过 ...出异常redis.clients.jedis.exceptions.JedisException 春暄并茂蓝玉联坚什么意思如题 谢谢了 java 开发在使用Redis时总是报这样的错误,重启就好 但是问题还是会出现... 春喧桃李是什么意思? 想读一个网络教育本科,学费多少合适 纸黄金与存金宝的区别,风险差别。 人大网络教育每学分多少学费? 网络教育收费有哪些? daily后面为什么要加basis on our daily life中life能不能用lives? 请帮忙翻译成英文,谢谢了。 on a stand-alone basis翻译成中文是什么意思 还得麻烦高人翻译 英语单词看天下的目录 daily与 everyday 都意为“日常的”时有什么区别啊?(比如词组搭配上的区... a monthly basis是什么意思啊? 急急急-话题作文“位置与目标”求范文,哪位大哥帮帮忙啊!! daily basis 用英语单词造句& English 厉害的来。 春喧桃李的意思? 暄能组成什么词&#xF573;&#xFE0F; 蒋春暄是什么人这个人到底是了不起的天才还是科学骗 柠檬加决明子 她是金庸心中的小龙女,曾拒绝过刘德华,如今依旧优雅从容。她是谁呢... 怎么解决could not get a resource from the pool 谁演的小龙女最经典? java一个catch捕获两个异常报错 急求一篇关于什么是教育 教育的重要性,要用英语写 鲜柠檬和决明子一起泡水可以减肥嘛 权利的游戏龙女扮演者 决明子+玫瑰花茶+柠檬片是不是有减肥的功效 英语作文---is important 求分享孙耀威《靠近我》mp3网盘下载地址 『狼王的士高-靠近我』这首歌有什么办法可以下载吗? 为什么更新了Win7系统后我原来的电脑系统的账号被停用了呢?_百度知 ... 求一首英文歌. 靠近我 tasty 靠近我 中文版 mp3 有没有人有庾澄庆《靠近 》mp3音乐百度网盘资源 谁有罗震环的《靠近》下载地址啊,要mp3格式的。