开发者社区> 问答> 正文

jredis的连接池 getResource报错?报错

配置是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实例。但是为什么就是用不了呢?

展开
收起
爱吃鱼的程序员 2020-06-08 21:30:36 661 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB


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

    引用来自“ksfzhaohui”的评论

    jedisPool获取的连接,释放连接也应该通过jedisPool,而不是直接 jedis.close()怎么释放连接?
    2020-06-08 21:30:54
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载