Skip to content

fix(transaction): connections not returned to pool after exec() or discard() in JedisPooled #4172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

raphaeldelio
Copy link

When using JedisPooled.multi(), the returned Transaction object borrows a connection from the pool. However, the connection would not be released after exec() or discard() was called. Over time, this led to connection exhaustion and blocked threads.

This change ensures that the connection is returned to the pool automatically by calling close() at the end of exec() and discard().

…scard() in JedisPooled

This ensures close() is called after exec() and discard(), allowing the connection to be returned to the pool when using JedisPooled.
@ggivo
Copy link
Collaborator

ggivo commented Jun 2, 2025

@raphaeldelio
Thanks for taking the time to report it.

Transaction/Pipeline API implementation in Jedis is a bit ambiguous, it allows reusing the same Transaction object for multiple transaction executions. Closing the connection on exec/discard is introducing a breaking change, and if we decide to go in that direction will need to introduce it in a major version. We will consider that for future improvement.

To resolve your issue, ‘After eight transactions, we run out of connections, ’ you need to either call Transaction#close manually, or use try-with-resources block. You can refer to the official example Example with try-with-resources

try ( AbstractTransaction trans = jedis.multi()) {
    trans.incrBy("counter:1", 1);
    trans.incrBy("counter:2", 2);
    trans.incrBy("counter:3", 3);
    trans.exec();
}

@raphaeldelio
Copy link
Author

@ggivo Got it! I thought I could be missing something. My intuition was that JedisPooled should take care of managing all resources. Thank you for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants