-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
JdbcTemplate.queryForStream() does not return connection to the pool, causing the connection leak. #27988
Comments
As stated in the Javadoc for
As you can see in the following code listing, the Stream returned has an spring-framework/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java Lines 840 to 847 in e352945
In light of that, I am closing this issue as "works as designed". As a side note, if you are retrieving a single value from the database, have you considered using In other words, why are you using a stream for a single result? |
Thanks for explanation. I thought that the stream terminal methods also close the internal connection. When I modified my example code to use try-with-resources it worked as expected. |
I am seeing the same issue. it is not that intuitive and I thought it is a bug for connection leak as well. What is the use case for |
I agree with other posters that this violates the principle of least surprise, I would never expect that I would have to close the connection because I don't do that with any other of the methods in that class. |
Adding Transaction annotation also helps, but I still think |
If you insist on having Note, however, that as of Spring Framework 6.1 the recommend way to have |
I don't believe we have an actual example in the documentation; however, you can see an example in our test suite: spring-framework/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java Lines 383 to 392 in 232225b
Feel free to open a new ticket to suggest that an example be included in the reference manual. |
Although it may be surprising, it is sometimes the case that one must close a For example, the
|
Affects: <Spring JDBC version 5.3.15>
Please refer to the demo project that replicates the issue.
When using
JdbcTemplate.queryForObject()
and subsequently calling the.findFirst()
method, the connection is not getting closed, hense the connection pool runs dry very quickly.The result of the execution of the following code is:
The text was updated successfully, but these errors were encountered: