java redis释放连接_redis在应用中使用连接不释放问题解决
今天測試,發(fā)現(xiàn)redis使用的時候,調(diào)用的鏈接一直不釋放。后查閱蠻多資料,才發(fā)現(xiàn)一個配置導(dǎo)致的。并不是他們說的服務(wù)沒有啟動導(dǎo)致的。
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)測試?yán)?/p>
寫了一個springmvc的controller類,然后調(diào)用線程使用連接,出現(xiàn)問題。
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;/*** 跳轉(zhuǎn)方式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("完成");
}
}
出現(xiàn)問題異常: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)查看鏈接數(shù)
通過客戶端工具到服務(wù)器去查詢當(dāng)前連接數(shù):當(dāng)前10個
[root@dev2 bin]# ./redis-cli info clients
# Clients
connected_clients:10client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0
4)分析問題
因為我們設(shè)置最初的連接數(shù)最大是9個,加上我自己通過客戶端訪問連接數(shù)10個,理論上應(yīng)該釋放才對,這里沒有釋放,是有問題的。因為這個鏈接應(yīng)該是與數(shù)據(jù)庫鏈接一樣,會釋放,才能長久。。。
間隔很久訪問,依舊是10個。沒有釋放。一旦有httprequest請求發(fā)出來,錯誤依舊是:沒有取到鏈接。
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)修改配置
經(jīng)過反復(fù)查找屬性,最終在配置文件中發(fā)現(xiàn)一個配置,是事務(wù)處理的,網(wǎng)上查詢得知,如果啟動了redis中的事務(wù)管理,必須使用mul和execute執(zhí)行后才能生效。而我們這里沒有使用這個事務(wù)。so去掉這個配置。
6)重新測試
重新部署,啟動,多次刷新后連接數(shù)都沒有出現(xiàn)無法獲取的異常,很正常。
# Clients
connected_clients:1client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0
7)問題解決
總結(jié):這個配置項,需要注意。。
總結(jié)
以上是生活随笔為你收集整理的java redis释放连接_redis在应用中使用连接不释放问题解决的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java linkedlist源码_Ja
- 下一篇: java内部类为什么会持有外部类的引用_