SpringBoot Service全局变量创建连接,关闭异常问题原因与解决

本文介绍了在SpringBoot应用中,使用全局变量Jedis进行Redis操作时遇到的关闭异常问题。当前端查询量增大或查询时间过长时,由于Jedis连接超时或未正确关闭,导致查询失败。解决方案包括设置bean的scope属性为prototype,每次请求创建新的Jedis实例,或者采用按需创建和释放的方式确保连接管理的正确性。
摘要由CSDN通过智能技术生成

业务场景步骤:

  1. 前端获取JSON参数
  2. 封装成ES查询语句,HttpClient的RestClient低阶API查询ES方式查询
  3. 查询ES表数据
  4. ES表数据逐条匹配redis数据,处理数据
  5. 封装成AOP统一约定的JSON返回体

出错时间:

  1. 前端查询大约100条以内
  2. 前端查询大约第二天

错误示例:

16:03:22.206 [http-nio-8888-exec-3] ERROR xx.aop.advice.LogAdvice - This is after throw from LogAdvice...redis.clients.jedis.Connection.disconnect(Connection.java:182)
16:03:22.211 [http-nio-8888-exec-3] INFO  xx.aop.advice.LogAdvice - ==========================End==============================
16:03:22.270 [http-nio-8888-exec-3] ERROR xx.aop.exception.GlobalExceptionHandler - 褰撳墠璇锋眰URL鎶ラ敊...http://192.168.211.106:8888/api/searchDomainRouteInfo
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
        at redis.clients.jedis.Connection.disconnect(Connection.java:182) ~[jedis-3.3.0.jar!/:?]
        at redis.clients.jedis.BinaryClient.disconnect(BinaryClient.java:936) ~[jedis-3.3.0.jar!/:?]
        at redis.clients.jedis.Connection.close(Connection.java:172) ~[jedis-3.3.0.jar!/:?]
        at redis.clients.jedis.BinaryClient.close(BinaryClient.java:942) ~[jedis-3.3.0.jar!/:?]
        at redis.clients.jedis.BinaryJedis.close(BinaryJedis.java:1944) ~[jedis-3.3.0.jar!/:?]
        at redis.clients.jedis.Jedis.close(Jedis.java:3524) ~[jedis-3.3.0.jar!/:?]
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88) ~[spring-aop-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at sun.reflect.GeneratedMethodAccessor48.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_251]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_251]
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644) ~[spring-aop-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633) ~[spring-aop-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) ~[spring-aop-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值