通过jedis客户端连接redis写入报"JedisConnectionException: java.net.SocketException: Software caused connection

在使用Jedis客户端连接Redis进行并发写入时,出现'JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error'异常。问题源于并发使用同一Jedis实例导致的错误。为解决并发问题和避免过多的socket连接,建议使用线程安全的JedisPool。具体实现可参考提供的工具类代码。
摘要由CSDN通过智能技术生成

通过jedis客户端连接redis写入报"JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error"异常

最近开发毕设时,使用到了redis,由于服务器是学生机配置,所以没有做redis集群处理,只是使用单例jedis做各种redis处理,但是使用这种方法确实会遇到并发问题,通过客户端jedis连接redis服务器并发写入数据时报出“redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error…Caused by: java.net.SocketException: Software caused connection abort: socket write error”等异常日志,详情如下所示:


redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error
[xwood-pms-publish]  [pool-2-thread-1] 2017-06-05 13:31:08,246 - com.xwood.publisher.master.PublishEntityDataHandler -61998 [pool-2-thread-1] INFO com.xwood.publisher.master.PublishEntityDataHandler  -  【 extract - PublishEntityDataHandler:1】  s1-2@ entityNo:447@349779 .... 
    at redis.clients.jedis.Protocol.sendCommand(Protocol.java:94)
    at redis.clients.jedis.Protocol.sendCommand(Protocol.java:74)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:80)
    at redis.clients.jedis.BinaryClient.set(BinaryClient.java:92)
    at redis.clients.jedis.Client.set(Client.java:24)
    at redis.clients.jedis.Jedis.set(Jedis.java:62)
    at com.xwood.publisher.util.JedisUtil.set(JedisUtil.java:35)
    at com.xwood.publisher.master.PublishEntityDataHandler.pushRemoteRedis(PublishEntityDataHandler.java:43)
    at com.xwood.publisher.master.PublishEntityDataHandler.run(PublishEntityDataHandler.java:36)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)
[xwood-pms-publish]  [pool-2-thread-1] 2017-06-05 13:31:08,247 - com.xwood.publisher.master.PublishEntityDataHandler -61999 [pool-2-thread-1] INFO com.xwood.publisher.master.PublishEntityDataHandler  -  【extract - PublishEntityDataHandler:1】  s1-3@ entityNo:447@349779 .... 
Caused by: java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:31)
    at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:38)
    at redis.clients.jedis.Protocol.sendCommand(Protocol.java:80)
    ... 16 more

之前创建jedis实例如下:

    static {
   
        jedis = new Jedis("xxx.xxx.xxx.xxx",6379,300000);
        jedis.auth("chen8999958...");
    }

在不同的线程中使用相同的Jedis实例会发生奇怪的错误。但是创建太多的实现也不好因为这意味着会建立很多sokcet连接,也会导致奇怪的错误发生。单一Jedis实例不是线程安全的。为了避免这些问题,可以使用JedisPool, JedisPool是一个线程安全的网络连接池。可以用JedisPool创建一些可靠Jedis实例,可以从池中拿到Jedis的实例。这种方式可以解决那些问题并且会实现高效的性能. 具体实现看如下工具类代码:

package cn.alias.shop.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;
import java.io.IOException;
impo
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值