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
Closing Connection Does Not Close Statement And ResultSet #1903
Closing Connection Does Not Close Statement And ResultSet #1903
Comments
While they may close them this is not a requirement of the spec as far as I can tell. https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#close() |
I googled and found some links that say it should be closed but then there are people that say the statement and resultset should be assumed to be closed by closing the connection. The first answer says it should be closed: Not the most popular answer but... from Enerccio: In any case, whether it is in the JDBC spec of not, it would be NICE to have the connection close() also close the statement and resultset. It seems like some other drivers already do that (MySQL) and having PG implement this feature would be really neat, as that would facilitate just using try-with-resources on the Connection object itself and have it clean up everything (con, stmt, rs) once that block of code is done. try(Connection con = DriverManager.getConnection(dbInfo[0], dbInfo[1], dbInfo[2])) { Thank you. |
Note that https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#close() |
Thanks Dave for the link. I tested closing the statement but the resultset object was still open (isClosed() = false). So I think this particular issue is a genuine bug. To answer your question, as to why, first I thought it was required for the connection to close the stmt and rs so I thought it was a bug. From a certain point of view, one could think that whatever resources are closed in the pg implementation of resultset are "JDBC resources" and therefore should be closed. But let's say it's not required, then I would like to change the connection issue to a suggestion. Mainly I do not want to write close() on resultset and stmt objects and add a try/catches around then, when instead I could write a try-with-resources on the connection object and that would close all three objects after the block finishes. It would make the code less verbose and more elegant, in my opinion. And it would also duplicate the behavior in other drivers which I think provides a convenience. |
…first unclosed result fixes pgjdbc#1903
It's sort of actually closed internally. We set it to null in the statement. The object is invalid but the state is "open" I have a PR to fix it see above |
Thanks Dave. It looks good to me. |
Describe the issue
(1) Closing the connection object does not close the statement and resultset objects, which it should according to what I read online about JDBC.
(2) Closing the statement object does not close the resultset object.
I tried a different database and driver (MySQL) and verified that the MySQL JDBC driver DOES close these two objects once the connection is closed.
Driver Version?
42.2.16
Java Version?
openjdk version "11.0.7" 2020-04-14
OS Version?
MacOS Catalina - 10.15.6
PostgreSQL Version?
Docker image - "postgres:13"
To Reproduce
Steps to reproduce the behaviour:
Expected behaviour
A clear and concise description of what you expected to happen.
And what actually happens
Closing connection object closes the statement and resultset objects but what actually happens is that the statement and resultset object are still open (isClosed() returns false).
Closing the statement object closes the resultset object but what actually happens is that the resultset object is still open (isClosed() returns false).
Logs
If possible PostgreSQL logs surrounding the occurrence of the issue
Additionally logs from the driver can be obtained adding
to the connection string
Using the following template code make sure the bug can be replicated in the driver alone.
The text was updated successfully, but these errors were encountered: