jredis的连接池 getResource报错

plugin 发布于 2016/10/07 01:00
阅读 467
收藏 0

配置是MaxTotal=20.

netstat -ano | findstr "6379" 发现没有连接

测试:

    不停的刷新,直到 netstat -ano | findstr "6379" 执行后,发现已经有20个连接了

这时候,如果再刷新页面,至此以后,都会报下面这个错误。说是找不到实例了。

redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
	at redis.clients.util.Pool.getResource(Pool.java:51)
	at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
	at com.hao.comm.utils.RedisUtil.getJedis(RedisUtil.java:54)
	at com.hao.comm.utils.RedisUtil.set(RedisUtil.java:98)
	at com.hao.pet.user.service.PermissionServiceImpl.getRoleList(PermissionServiceImpl.java:125)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
	at com.sun.proxy.$Proxy27.getRoleList(Unknown Source)
	at com.hao.back.user.UserPermissionController.showRoleList(UserPermissionController.java:45)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:743)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:672)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:82)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:933)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:867)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:951)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:842)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:827)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at com.hao.comm.filter.DefaultFilter.doFilter(DefaultFilter.java:23)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at com.hao.comm.filter.ThreadFilter.doFilter(ThreadFilter.java:34)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)



我的代码里是这么用

private Jedis getJedis() {
        return jedisPool.getResource();
    }
   
 public Object get(String key) {
        Jedis jedis = getJedis();
        try {
            byte[] value = jedis.get(key.getBytes());
            return value == null ? null : SerializeUtil.deserialize(value);
        } finally {
            cleanup(jedis);
        }
    }

    private void cleanup(Jedis jedis) {
        if (jedis != null) jedis.close();
    }



我觉得我的代码是没有问题的,是正确的释放了Jedis实例。但是为什么就是用不了呢?

加载中
0
Moodys
Moodys


redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:51)
看异常是说:无法从连接池中获取资源, 初步判断 jedisPool没有设置对!


代码没贴全吧, 还是本来就没写全? jedisPool 要设置连接信息的, 比如数据库地址,端口....

JedisPool创建代码
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");

    

at com.hao.comm.utils.RedisUtil.getJedis(RedisUtil.java:54)

    at com.hao.comm.utils.RedisUtil.set(RedisUtil.java:98)

这两行抛的异常, 贴下代码!

0
ksfzhaohui
ksfzhaohui
jedisPool获取的连接,释放连接也应该通过jedisPool,而不是直接jedis.close()
ksfzhaohui
ksfzhaohui
回复 @plugin : 确实,现在可以直接用jedis.close()了;那只能是没有释放或者是没有释放及时,还有就是这种反序列化的操作最好不要放在get方法里面,这样会延迟释放连接
追月小虎
追月小虎
jedisPool怎么释放连接???
plugin
plugin
不是的吧。新版的推荐用jedis.close方法释放连接
0
Moodys
Moodys

引用来自“ksfzhaohui”的评论

jedisPool获取的连接,释放连接也应该通过jedisPool,而不是直接jedis.close()
说的对
追月小虎
追月小虎
怎么释放连接?
返回顶部
顶部