使用Jedis在高并發(fā)報(bào)錯(cuò) (java.net.SocketException: Connection reset by peer: socket write error)

使用Jedis在高并發(fā)報(bào)錯(cuò) (java.net.SocketException: Connection reset by peer: socket write error)

1.報(bào)錯(cuò)信息

java.lang.reflect.InvocationTargetException: null
    at sun.reflect.GeneratedMethodAccessor15.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    ....
    at java.lang.Thread.run(Unknown Source)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Connection reset by peer: socket write error
    at redis.clients.jedis.Connection.flush(Connection.java:334)
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:257)
    at redis.clients.jedis.BinaryJedis.get(BinaryJedis.java:244)
    ......
    ... 15 common frames omitted
Caused by: java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:52)
    at redis.clients.util.RedisOutputStream.flush(RedisOutputStream.java:216)
    at redis.clients.jedis.Connection.flush(Connection.java:331)
    ... 22 common frames omitted

Connection reset by peer: socket write error錯(cuò)誤分析:
常出現(xiàn)的Connection reset by peer: 原因可能是多方面的,不過更常見的原因是:
①:服務(wù)器的并發(fā)連接數(shù)超過了其承載量,服務(wù)器會(huì)將其中一些連接Down掉;
②:客戶關(guān)掉了瀏覽器,而服務(wù)器還在給客戶端發(fā)送數(shù)據(jù);
③:瀏覽器端按了Stop

所以本問題是由 ①造成的

修改之前的代碼

初始化jedis的代碼


  /**
    * 在多線程環(huán)境同步初始化
    */
   private static synchronized void poolInit() {
       if (pool == null) {
           createJedisPool();
       }

   }

  * 獲取一個(gè)jedis 對(duì)象
    *
    * @return
    */
   public static Jedis getJedis() {

       if (pool == null) {
           poolInit();
       }
   
       return pool.getResource();
      
        
   }

使用jedis的代碼

 private static Jedis jedis = JedisPoolUtil.getJedis();
 
 public static Object getObject(String key) {
       if(exists(key)){
           return deserialize(jedis.get(key.getBytes()));
       }
       return null;
   }

修改之后的代碼

初始化jedis的代碼

   /**
    * 獲取一個(gè)jedis 對(duì)象
    *
    * @return
    */
   public static Jedis getJedis() {

       if (pool == null) {
           poolInit();
       }
       //如果沒有以下代碼會(huì)造成初始化的jedis拿不到 jedis對(duì)象
       Jedis jedis = null;
       try {
           if (pool != null) {
               jedis = pool.getResource();
           }
       }
       catch (Exception e) {
           logger.error("獲取redis失敗 : {}" + ExceptionUtils.getStackTrace(e));
       }
       return jedis;
   }

使用jedis的代碼

/**
    * 讀取對(duì)象
    *
    * @param key
    * @return
    */
   public static Object getObject(String key) {
       if (exists(key)) {
           //初始化jedis用完之后關(guān)閉連接
           Jedis jedis = JedisPoolUtil.getJedis();
           Object object = deserialize(jedis.get(key.getBytes()));
           jedis.close();
           return object;
       }
       return null;
   }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容