欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分布式实现redis锁 出现错误Could not get a resource from the pool

程序员文章站 2022-03-06 13:34:57
...
具体错误信息:
Exception in thread "d窗口" 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
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:286)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:469)
at com.redies.demo.lock.RediesLock.tryLock(RediesLock.java:56)
at com.redies.demo.lock.RediesLock.lock(RediesLock.java:39)
at com.redies.demo.lock.RediesLockTest$TicketRunner.run(RediesLockTest.java:50)
at java.lang.Thread.run(Thread.java:745)
Caused by: redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:51)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:16)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:276)
... 5 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:448)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 8 more
Exception in thread "c窗口" 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
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:286)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:469)
at com.redies.demo.lock.RediesLock.tryLock(RediesLock.java:56)
at com.redies.demo.lock.RediesLock.lock(RediesLock.java:39)
at com.redies.demo.lock.RediesLockTest$TicketRunner.run(RediesLockTest.java:50)
at java.lang.Thread.run(Thread.java:745)
Caused by: redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:51)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:16)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:276)
... 5 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:448)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 8 more
解决方法:jedis设置最大活动数
connectionFactory.getPoolConfig().setMaxTotal(1000); 默认为8

项目环境:(具体代码如附件)
spring boot 2.0.1.RELEASE
spring-boot-starter-data-redis
用jedis操作redis,使用注解注册bean
@Configuration
public class RedisConfig {
    @Value("${redis.host}")
    private String hostName;
    @Value("${redis.port}")
    private String port;
    @Bean
    JedisConnectionFactory jedisConnectionFactory()
    {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(hostName);
        configuration.setPort(Integer.valueOf(port));
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration);
        connectionFactory.getPoolConfig().setMaxTotal(1000);//#最大活动对象数
        connectionFactory.getPoolConfig().setMaxWaitMillis(2000);
        return connectionFactory;

    }
具体代码如附件

最开始未设置connectionFactory.getPoolConfig().setMaxWaitMillis(2000);导致异常错误一直未打印,原因是BlockWhenExhausted( connectionFactory.getPoolConfig().setBlockWhenExhausted(false))的默认值为true,阻塞到超时,才会打印相应的错误

上一篇: Bean 的四种构造方式

下一篇: dfdfdf