2017-10-06 143 views
1

我正在創建Web應用程序並使用Redis進行數據存儲。 我已經使用JedisPoolConfig(redis client-jedis版本2.9.0)。 所以,當我測試我使用Apache的JMeter的幫助應用程序與以下配置:無法獲取Jedis連接,無法從池中獲取資源

Number of threads(Users): 30 
Ramp-Up period(in seconds) : 0 
Loop-Count : 1 

和JedisPoolConfig具有以下配置:

redis.sentinel.enable=true 
redis.sentinel.ip=localhost 
redis.sentinel.port=6379 
redis.instance.account.masterName=master 
redis.pool.max-active = 20 
redis.pool.max-idle = 10 

與此代碼(這是因爲豆上創建Spring應用程序的啓動):

JedisConnectionFactory factory = new JedisConnectionFactory(); 
     factory.setPort(port); 
     factory.setHostName(hostName); 
     factory.afterPropertiesSet(); 
     factory.setUsePool(true); 
     JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 
     jedisPoolConfig.setMaxTotal(redisPoolMaxActive); 
     jedisPoolConfig.setMaxIdle(redisPoolMaxIdle); 
     factory.setPoolConfig(jedisPoolConfig); 
     return factory; 

與此代碼每次取新連接的請求來了:

@Autowired 
private JedisConnectionFactory redisConnectionFactory; 
private StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); 
private RedisTemplate getNewConnection(){ 
     RedisTemplate redisTemplate = new RedisTemplate(); 
     redisTemplate.setConnectionFactory(redisConnectionFactory); 
     redisTemplate.setKeySerializer(stringRedisSerializer); 
     redisTemplate.setHashKeySerializer(stringRedisSerializer); 
     redisTemplate.setValueSerializer(stringRedisSerializer); 
     redisTemplate.afterPropertiesSet(); 
     return redisTemplate; 
    } 

編輯:我使用的redisTemplate多/ EXEC內的Redis:

redisTemplate.setEnableTransactionSupport(true); 

redisTemplate.multi(); 

for (ArrayList<Pair<String, String>> pairs : keys){ 
    for (Pair<String,String> pair: pairs) 
     redisTemplate.opsForHash().get(makeMap(pair.getFirst(), version) , pair.getSecond()); 
} 

return redisTemplate.exec(); 

我順利拿到了20個請求的響應,但不能得到休息10所請求的響應。我現在面臨的問題是,經過20個連接已經使用了前20條的要求,其餘10個請求無法從池中獲取的jedis連接,因此我得到以下異常:

2017-10-06 17:38:11.753 ERROR 3539 --- [io-8080-exec-12] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool] with root cause 

java.util.NoSuchElementException: Timeout waiting for idle object 
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:449) ~[commons-pool2-2.4.2.jar:2.4.2] 
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) ~[commons-pool2-2.4.2.jar:2.4.2] 
    at redis.clients.util.Pool.getResource(Pool.java:49) ~[jedis-2.9.0.jar:na] 
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226) ~[jedis-2.9.0.jar:na] 
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:16) ~[jedis-2.9.0.jar:na] 
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:194) ~[spring-data-redis-1.8.6.RELEASE.jar:na] 
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:348) ~[spring-data-redis-1.8.6.RELEASE.jar:na] 
    at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:129) ~[spring-data-redis-1.8.6.RELEASE.jar:na] 
    at org.springframework.data.redis.core.RedisConnectionUtils.bindConnection(RedisConnectionUtils.java:67) ~[spring-data-redis-1.8.6.RELEASE.jar:na] 
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:192) ~[spring-data-redis-1.8.6.RELEASE.jar:na] 
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169) ~[spring-data-redis-1.8.6.RELEASE.jar:na] 
    at org.springframework.data.redis.core.RedisTemplate.multi(RedisTemplate.java:868) ~[spring-data-redis-1.8.6.RELEASE.jar:na] 
    at net.media.mml.redis.repository.RedisRepository.makeTransaction(RedisRepository.java:50) ~[max-data-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] 
    at net.media.mml.redis.repository.RedisRepository.getFromRedis(RedisRepository.java:66) ~[max-data-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] 
    at net.media.mml.service.DimensionGetter.getDimensions(DimensionGetter.java:64) ~[ml-api.jar:0.0.1-SNAPSHOT] 
    at net.media.mml.service.MLQueriesGetter.makeQueries(MLQueriesGetter.java:32) ~[ml-api.jar:0.0.1-SNAPSHOT] 
    at net.media.mml.controller.MaxQueryController.query(MaxQueryController.java:61) ~[ml-api.jar:0.0.1-SNAPSHOT] 
    at sun.reflect.GeneratedMethodAccessor270.invoke(Unknown Source) ~[na:na] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131] 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] 

我已搜查通過網絡,但無法找到解決方案。有誰能夠幫助我?

+0

請發佈完整的堆棧跟蹤,因爲它是從你的應用程序。 – mp911de

+0

@ mp911de,我編輯了完整的例外。 –

+0

我也在網上找到了這個配置:jedisPoolConfig.getBlockWhenExhausted();這個參數是通過deault來實現的。但似乎jedispoolconfig實際上並沒有阻止所有連接使用中的請求。 –

回答

0

使用Redis事務需要在回調中調用。 setEnableTransactionSupport(true)用於交易管理器/ @Transactional的使用。有關更多詳細信息,請參見reference documentation

除非你有一個事務管理器,否則不要使用setEnableTransactionSupport(true)

你的代碼應該是這樣的:

​​
相關問題