spring-projects / spring-projects/spring-data-redis

JedisConnection.scan releases the connection instance to the pool [DATAREDIS-604]

Open
#1,179 3 comments 0 reactions 1 assignee View on GitHub

@mp911de is already working on this.

Since Dec 30, 2020.

in: jedis in: lettuce type: bug
Dominant language
Java
Stars
1.9k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Stefan Müller opened DATAREDIS-604 and commented

When using JedisConnection.scan to iterate over the redis database keys and closing the cursor afterwards the JedisConnection is closed and released to the pool. If you try to release the connection afterwards an exception is thrown.

long count = 0;
RedisConnection connection = null;
try {
	connection = RedisConnectionUtils.getConnection(redisConnectionFactory);
	try (Cursor<byte[]> entries = connection
			.scan(ScanOptions.scanOptions().match(REFRESH + "*").count(1000).build())) {
		while (entries.hasNext()) {
			count++;
			entries.next();
		}
	} catch (IOException e) {
		log.warn("error closing redis cursor", e);
	}
} finally {
	if ((connection != null) && (!connection.isClosed())) {
		RedisConnectionUtils.releaseConnection(connection, redisConnectionFactory);
	}
}

I tracked down the problem to the following pull request:
https://github.com/spring-projects/spring-data-redis/pull/218

I understand that the connections need to be released when scan is used from RedisTemplate.getXXXOperations - but when using the connection directly releasing the connection should be done by the caller.

As a workaround we must catch the exception which is thrown

redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
Caused by: java.lang.IllegalStateException: Object has already been returned to this pool or is invalid

to make our code work with spring-data-redis 1.7 and 1.8 because there is no way to detect if a connection is already returned to the pool / factory.

I would like to know if this is a bug or intended behavior. If it is intended behavior it should be documented, that closing the scan-cursor closes the underlying connection and the caller needs to make a new connection to continue


Affects: 1.8 GA (Ingalls)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.