squid:S2095 "Resources should be closed"

127 views
Skip to first unread message

jessica...@gmx.de

unread,
Jul 10, 2017, 7:37:11 AM7/10/17
to SonarQube
Generally I would totally agree to this rule. But in the case of a ResultSet I am convinced this rule is too strict, because ResultSets are closed automatically when the Statement is closed, see https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html (last but one paragraph): "A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results." (last but one paragraph). Rule S2095 in the case of a ResultSet is unnecessary and leads to superfluous code.
Message has been deleted

jessica...@gmx.de

unread,
Jul 10, 2017, 7:58:21 AM7/10/17
to SonarQube, jessica...@gmx.de
SonarQube 6.4, SonarJava 4.11.0.10660; PreparedStatement in a try-with-resources block.

G. Ann Campbell

unread,
Jul 10, 2017, 9:20:57 AM7/10/17
to SonarQube, jessica...@gmx.de
Hi, 

First, please be aware that the standard courtesies (Hi, Thanks, ...) are appreciated in this group. 

While it's true that ResultSet should be closed automatically with the Connection, it's also true that it might not be (I've seen this happen) and in any case it should be closed as soon as you're done with it for optimum performance. Or at lease, that's what I get from StackOverflow.

Besides, with Java 7 you can establish it in a try-with-resources and it will (really!) be closed for you automatically.


Ann

jessica...@gmx.de

unread,
Jul 10, 2017, 10:54:01 AM7/10/17
to SonarQube, jessica...@gmx.de
Hi,

 thank you for your  instruction about courtesy - yes, indeed, I have overseen this totally because I was puzzled about the new issues Sonarqube suddenly found in really old code.

Nevertheless - I think this suspender and belt programming for eventually buggy drivers is exaggerated. To avoid the masses of boilerplate code, try-with-resources and autocloseable was developed, normally one can trust the API doc. And closing the PreparedStatement follows in most cases immediately after the ResultSet is not used any more - how many milliseconds will you save closing it by hand? I am not talking about a Connection; closing it do not close the PreparedStement and do not close the ResultSet.

MeanwhiIe I did some more research on the Sonarqube bugtracker and in this group (initially I assumed a false issue ID), and it seems to be a  known bug: https://jira.sonarsource.com/browse/SONARJAVA-2060

Sincerely,
Jessica

G. Ann Campbell

unread,
Jul 10, 2017, 11:52:30 AM7/10/17
to SonarQube, jessica...@gmx.de
Hi Jessica,

Indeed. :-]


Ann
Reply all
Reply to author
Forward
0 new messages