Skip to content

No connections in poll available when performing multiple queries in parallel #1000

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

Closed
zygisx opened this issue Jul 2, 2021 · 4 comments
Closed
Assignees
Labels
type: documentation A documentation update

Comments

@zygisx
Copy link

zygisx commented Jul 2, 2021

When performing n repository queries in parallel, where n >= maximumPoolSize, all queries seems to be stuck and hikaricp pool do not have free connections. Getting error:

HikariPool-1 - Connection is not available, request timed out after 30002ms.

Preconditions:

  1. Selecting aggregate entity with 1-n relationship
  2. @Trasactional annotation is not added

I have made small demo project to showcase this problem:
https://github.com/zygisx/demo-spring-data-jdbc-problem

As you can see database aggregate contains 1-n relationship UserEntity -> UserEmailAddress: link

Whenever i add @Transactional annotation to repository problem is gone. Though spring-data-jdbc documentation states that:

CRUD methods on repository instances are transactional by default
https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.transactions

Tests in demo project showcases this issue
https://github.com/zygisx/demo-spring-data-jdbc-problem/blob/main/src/test/kotlin/com/example/demo/DemoApplicationTests.kt

When hikaricp maximumPoolSize is 10. Tests which performs 9 parallel calls to Repository passes.
Though test which performs 10 parallel calls to repository fails with HikariPool-1 - Connection is not available, request timed out after 30002ms..

If You see that i can provide more info regarding this issue feel free to contact me.
Thanks for looking into it ;)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 2, 2021
@schauder
Copy link
Contributor

schauder commented Jul 2, 2021

You don't have a @Transaction annotation, this means for every query a separate transaction is opened and for it a connection requested from the pool. Since Spring Data JDBC needs multiple queries for the method in question you have multiple threads trying to obtain connections in random order. This leads to deadlocks.

The fix should be simply to add @Transactional to the repository method.
The fix should result in either all method calls properly finishing or some of them getting a proper exception when the pool is exhausted.

Could you please verify, that the method works with proper transaction configuration?

@schauder schauder added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 2, 2021
@schauder schauder self-assigned this Jul 2, 2021
@schauder
Copy link
Contributor

schauder commented Jul 2, 2021

Sorry, I saw you actually wrote, that it works with transactions.

About the documentation saying

CRUD methods on repository instances are transactional by default

Yeah, I guess that is misleading. This refers only to the methods provided by the CrudRepository interface.

@zygisx
Copy link
Author

zygisx commented Jul 2, 2021

Yes, as I wrote with @Transactional it works.

So, if understood correctly, if no @Transactional annotation added to repository method, for each db query new connection will be obtained. And those connections will be released only after all queries will be finished.
So having big aggregate which performs many queries will use significant portion of database connection pool connections to retrieve aggregate from database, when no @Transactional added.
(please correct me, if my assumptions is wrong)

Is there a reason why Transactional behaviour is not a default one ?

Thanks for quick response ;)

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 2, 2021
@schauder
Copy link
Contributor

schauder commented Jul 2, 2021

Yes, your understanding is correct. I'll take the question about the default behaviour to the team.

@schauder schauder added status: waiting-for-triage An issue we've not yet triaged and removed status: feedback-provided Feedback has been provided labels Jul 2, 2021
@schauder schauder added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants