Redis集成SpringDataRedis问题:Could not get a resource from the pool
今天在Redis集成SpringDateRedis的案例上遇到了一个问题:
HTTP Status 500 - Request processing failed; nested exception is com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method findByCategoryId in the service
后面的报错原因貌似是尝试了三次超时,我一开始以为是RedisTemplate没有注入成功,但是仔细查看配置文件发现bean都配置的好好的。重开一个test项目后,使用JUnit集成spring测试SpringDataRedis,发现它报错换了一个方式,就是标题所说的:Could not get a resource from the pool,大概的意思是无法从池子里获得资源。
在网上查阅资料后,发现SpringDataRedis在使用RedisTemplate后是不会自动释放资源的,而资源都被占用自然获取不到连接对象,所以有几种方法可以使用:
- 暴力法:重开redis服务器
- 采用工具类:工具类包装一个方法关闭连接对象,在使用完资源后调用释放
- spring aop:采用切面方法,包装一个after方法,自动在方法使用后调用释放资源