Spring RedisTemplate:8次调用方法键后挂断 [英] Spring RedisTemplate: after 8 calls method keys hangs up

查看:65
本文介绍了Spring RedisTemplate:8次调用方法键后挂断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring RedisTemplate (spring-data-redis 1.7.1) 与 Redis 进行通信.我需要通过正则表达式获取然后删除键(例如:context:user1:*").我使用方法 "RedisTemplate.keys(...)" 获取键数组

I use Spring RedisTemplate (spring-data-redis 1.7.1) for communication with Redis. I need to get and then delete keys by regexp (ex: "context:user1:*"). I use method "RedisTemplate.keys(...)" for getting array of keys

{
    String key = String.format("%s:%s:%s", context, userId,"*");
    Set<byte[]> keys = redisTemplate.getConnectionFactory().getConnection().keys(key.getBytes());
    logger.debug(String.format("test log"));
}

但是在 8-9 次迭代调用 restTemplates.keys(...) 停止执行我的 java 服务.方法的调用不是从框架返回的.我的服务挂了.而且每次都在发生.解决方法是仅重新启动我的服务.

But on 8-9 iteration call restTemplates.keys(...) stops execution of the my java service. The call of method is not returned from framework. My service is hangs up. Also it is happening everytime. Workaround is only restart my service.

推荐答案

假设您将 Jedis 与池化一起使用,您会遇到底层连接池耗尽的情况.

Assuming you're using Jedis with pooling, you run into an exhaustion of the underlying connection pool.

每次调用 redisTemplate.getConnectionFactory().getConnection() 都会从连接池中分配一个新连接.你调用 connection.close() 吗?如果没有,池就会耗尽.池开始阻塞您的请求(希望另一个线程将返回一个连接,以便请求连接的线程可以使用它).

Each call to redisTemplate.getConnectionFactory().getConnection() allocates a new connection from the connection pool. Do you call connection.close()?. If not, the pool gets exhausted. The pools starts to block your request (hoping another thread will return a connection so it can be used by the thread which requests a connection).

这篇关于Spring RedisTemplate:8次调用方法键后挂断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆