I have a Java application which uses JDBI for my database code. I have many DAOs which use roughly the following construction for queries:
return handle.createQuery(sql)
.bind("variable", variable)
.mapToBean(Bean.class)
.list();
Where handle
is a JDBI Handle
object. I am closing the handle elsewhere in the application, at the end of the transaction.
I scanned my code with SonarQube and it is complaining that I am opening JDBI Query
objects, which implement AutoCloseable
, without closing them. SonarQube suggests closing them in a try-with-resources, like so:
try(Query query = handle.createQuery(sql)){
return query.bind("variable", variable)
.mapToBean(Bean.class)
.list();
}
I am concerned that this may close the underlying handle or other resources I need for other queries happening inside the transaction. I am also skeptical that they need to be closed. Why does JDBI make these Query objects closeable? I have not found anything in the documentation indicating what resources they open. Is SonarQube correct or can this be safely ignored?
Thanks.