SelectJoinStep should be closed (Sonar)

Viewed 29

Whenever I create a method that returns a SelectJoinStep, I always get a Sonar issue. Is the sonar report correct?

When I use the select directly I don't get a sonar error report.

protected SelectJoinStep<Record> createSelect() {
      return getDslContext().select( selectFields ).from( getTable() );
   }

"Use try-with-resources or close this "SelectJoinStep" in a "finally" clause. Resources should be closedjava:S2095"

Is it recommended to close the step?

1 Answers

Background on the problem

This is obviously a false positive by Sonar. Numerous static analysis tools treat AutoCloseable as a "must close" or "should close", neglecting the fact that since Java 8, the contract is more of a hint than a formal contract.

The change in Java 8 must have been made because Stream<T> extends AutoCloseable, which in my opinion was a design mistake. There should have been a new API for optionally closeable types, leaving AutoCloseable mandatory, adding a CloseableStream type for those cases where the resource semantics is important. Likewise, a CloseableIterator would be useful as well, along with a try-foreach-with-resources statement.

Alas, that's not what we have, now.

Remedies in jOOQ

In any case, in order to prevent such issues, jOOQ has changed the API incompatibly, twice:

In both cases, the closeable use cases were very rare and didn't justify continuing "polluting" the type hierarchy with the AutoCloseable contract in general. Details can be seen in the above issue links.

If you cannot upgrade to jOOQ 3.17, then you should ignore the rule in Sonar, at least for jOOQ API.

Related