日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java redis释放连接_redis在应用中使用连接不释放问题解决

發布時間:2024/10/6 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java redis释放连接_redis在应用中使用连接不释放问题解决 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天測試,發現redis使用的時候,調用的鏈接一直不釋放。后查閱蠻多資料,才發現一個配置導致的。并不是他們說的服務沒有啟動導致的。

1)配置文件

#redis連接配置===================start=========================# Redis settings

redis.host=192.168.10.102redis.port=6379redis.pass=redis.maxIdle=1redis.maxActive=9redis.maxWait=1000redis.testOnBorrow=true#redis連接配置===================end=========================

2)測試例子

寫了一個springmvc的controller類,然后調用線程使用連接,出現問題。

DemoMvcController.java

packagecom.iafclub.demo.web.controller;importjavax.servlet.http.HttpServletRequest;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.redis.core.StringRedisTemplate;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importcom.iafclub.baseTools.util.MyDateUtil;

@Controllerpublic classDemoMvcController {

@AutowiredprivateStringRedisTemplate stringRedisTemplate;/*** 跳轉方式3

**/@RequestMapping("/testRedis.do")public voidtestRedis(Model model, HttpServletRequest request){

System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");for(int i=0;i<5;i++){

Thread thread= newRedisThread(stringRedisTemplate);

thread.setName("線程:" +i);

thread.start();

}

model.addAttribute("status", "完成"+MyDateUtil.getCurrentDateTimeStr());

System.out.println("完成");

System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

}

}

RedisThread.java線程類

packagecom.iafclub.demo.web.controller;importorg.junit.runner.RunWith;importorg.springframework.data.redis.core.BoundValueOperations;importorg.springframework.data.redis.core.StringRedisTemplate;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.iafclub.baseTools.util.MyDateUtil;

@RunWith(SpringJUnit4ClassRunner.class)public class RedisThread extendsThread {privateStringRedisTemplate redisTemplate;private String REVERSE_KEY = "batchJob:task_";publicRedisThread(StringRedisTemplate redisTemplate){this.redisTemplate =redisTemplate;

}

@Overridepublic voidrun() {//其實這里使用了多次,但是使用的也都是一個鏈接

for(int i=0;i<50;i++){

String value= Thread.currentThread().getName() + "{user:user"+MyDateUtil.getCurrentDateTimeStr()+";name:chenweixian"+System.currentTimeMillis()+"}";

redisTemplate.opsForValue().set(REVERSE_KEY+System.currentTimeMillis(), value);

redisTemplate.getConnectionFactory().getConnection().close();//BoundValueOperations opt = redisTemplate.boundValueOps(REVERSE_KEY+System.currentTimeMillis());//opt.set(value);//System.out.println(opt.get());

}System.out.println("完成");

}

}

出現問題異常:Cannot get Jedis connection

Exception in thread "線程:3"org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:162)

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:251)

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:58)

at com.iafclub.demo.web.controller.RedisThread.run(RedisThread.java:26)

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at redis.clients.util.Pool.getResource(Pool.java:50)

at redis.clients.jedis.JedisPool.getResource(JedisPool.java:88)

at redis.clients.jedis.JedisPool.getResource(JedisPool.java:12)

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:155)

...3more

Caused by: java.util.NoSuchElementException: Timeout waitingforidle object

at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442)

at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360)

at redis.clients.util.Pool.getResource(Pool.java:48)

...6 more

3)查看鏈接數

通過客戶端工具到服務器去查詢當前連接數:當前10個

[root@dev2 bin]# ./redis-cli info clients

# Clients

connected_clients:10client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0

4)分析問題

因為我們設置最初的連接數最大是9個,加上我自己通過客戶端訪問連接數10個,理論上應該釋放才對,這里沒有釋放,是有問題的。因為這個鏈接應該是與數據庫鏈接一樣,會釋放,才能長久。。。

間隔很久訪問,依舊是10個。沒有釋放。一旦有httprequest請求發出來,錯誤依舊是:沒有取到鏈接。

Exception in thread "線程:3" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

5)修改配置

經過反復查找屬性,最終在配置文件中發現一個配置,是事務處理的,網上查詢得知,如果啟動了redis中的事務管理,必須使用mul和execute執行后才能生效。而我們這里沒有使用這個事務。so去掉這個配置。

6)重新測試

重新部署,啟動,多次刷新后連接數都沒有出現無法獲取的異常,很正常。

# Clients

connected_clients:1client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0

7)問題解決

總結:這個配置項,需要注意。。

總結

以上是生活随笔為你收集整理的java redis释放连接_redis在应用中使用连接不释放问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。