MrR*_*ROY 5 java nosql redis jedis
我们的应用程序使用Jedis-2.2.1
并连接到Redis-2.6
,这就是我获取jedis资源的方法:
protected static JedisWrapper getRedisUserWrite(String UDID) {
if (redisUserWritePools.get(0) == null) init();
int hash = hash(UDID);
Jedis jedis = redisUserWritePools.get(hash).getResource();
jedis.select(dbs.get("redisUserWritePools" + hash));
return new JedisWrapper(jedis, redisUserWritePools.get(hash));
}
Run Code Online (Sandbox Code Playgroud)
这是我的JedisWrapper
(统一资源管理):
public class JedisWrapper {
private Jedis jedis;
private JedisPool pool;
public JedisWrapper(Jedis jedis, JedisPool pool) {
this.jedis = jedis;
this.pool = pool;
}
public Jedis get(){
return this.jedis;
}
public void returnResource() {
if(null != this.jedis){
this.pool.returnResource(this.jedis);
}
}
public void returnBrokenResource() {
if(null != this.jedis) {
this.pool.returnBrokenResource(this.jedis);
}
this.jedis = null;
}
}
Run Code Online (Sandbox Code Playgroud)
JedisWrapper
是Jedis实例的容器,这是我的使用方式:
private static void cacheSDKIDs(String UDID, String[] SDKIDs) {
JedisWrapper wrapper = getRedisUserWrite(UDID);
try {
if (SDKIDs != null) {
wrapper.get().del(UDID);
wrapper.get().sadd(UDID, SDKIDs);
}
} catch (JedisConnectionException e) {
e.printStackTrace();
wrapper.returnBrokenResource();
}catch (Exception e) {
e.printStackTrace();
} finally {
wrapper.returnResource();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,它SKDIDs
可能很大(例如,可能达到8KB的最大值)。
每次我重新启动应用程序时,所有的redis连接都是正常的,但是几个小时后,出现Could not get a resource from the pool
异常。并且频率越来越高,那么所有与Redis的连接都断开了,可以创建新的连接。
这是我的配置:
<bean id = "redisConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="400" />
<property name="maxIdle" value="100" />
<property name="minIdle" value="20" />
<property name="maxWait" value="4000" />
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true" />
</bean>
Run Code Online (Sandbox Code Playgroud)
异常Stacktrace:
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:40)
at com.xxxice.redis.BaseRedis.getRedisUserWrite(BaseRedis.java:158)
at com.xxx.service.redis.DeviceRedis.cacheSDKIds(DeviceRedis.java:128)
at com.xxx.redis.DeviceRedis.cacheDevice(DeviceRedis.java:65)
at com.xxx.service.DeviceService.update(DeviceService.java:88)
at com.xxx.controller.Devices.update(Devices.java:25)
... 32 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1174)
at redis.clients.util.Pool.getResource(Pool.java:38)
... 37 more
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12232 次 |
最近记录: |