redis use of connection pool connections are not released

In general, obtained by JedisPool connection Jedis, ran out and needs to close, so, when a subsequent application will need to be connected properly to get connected, no problem occurs in normal development, but today encountered a problem that when the connection is not closed, the connection pool of connections is exhausted, the program will get a connection abnormality occurs.

The maximum number of connections by default, is set in JedisPoolConfig 8, here to test the connection problem is not released, we will modify the configuration it is 4.

public static JedisPool jedisPool;
static{
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxTotal(4);
	config.setMaxIdle(200);
	config.setMaxWaitMillis(1000*100);
	config.setTestOnBorrow(false);
	jedisPool = new JedisPool(config, "127.0.0.1", 6379);
}

Then define a method of obtaining a queue element from the queue of redis:

public static List<String> brpop(int timeout,String key){
	Jedis jedis = null;
	try {
		jedis = jedisPool.getResource();
		return jedis.brpop(timeout,key);
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}finally{
		//jedis.close();
	}
}

Note here the finally closed connection code block, in order to simulate without closing the connection.

public static void main(String[] args) {
	new Thread(new Runnable() {			
		@Override
		public void run() {
			while(true){
				List<String> list = brpop(1000, "list");
				System.out.println("list value -> "+list.get(1));
			}
		}
	}).start();
}

The main function continuously acquired from simulation "list" queue in queue element, if the obtained value is printed out, if not wait. We expect the queue as long as there is a value, it will print, but the result is because the connection is not released, resulting in print after running into the fourth element, the program enters a wait state, regardless of whether there are elements in the queue.

Abnormal program error message: Could not GET A Resource from the pool at The

Here we jedis.close brpop method finally module () code comments open, you can achieve a similar message to the asynchronous queue. 

Guess you like

Origin blog.csdn.net/feinifi/article/details/92672813