为什么JDBI中的查询是可关闭的?

huangapple go评论68阅读模式
英文:

Why are Queries in JDBI closeable?

问题

我有一个Java应用程序,使用JDBI进行数据库编码。我有许多DAO,它们在查询中使用了大致以下的结构:

return handle.createQuery(sql)
             .bind("variable", variable)
             .mapToBean(Bean.class)
             .list();

其中handle是一个JDBI的Handle对象。我在应用程序的其他地方关闭了这个handle,在事务结束时关闭它。

我使用SonarQube扫描了我的代码,它指出我在没有关闭它们的情况下打开了实现了AutoCloseable接口的JDBI Query对象。SonarQube建议使用try-with-resources来关闭它们,像这样:

try(Query query = handle.createQuery(sql)){
    return query.bind("variable", variable)
                .mapToBean(Bean.class)
                .list();
}

我担心这可能会关闭底层的handle或其他我需要用于事务内发生的查询的资源。我也怀疑它们是否需要被关闭。为什么JDBI使这些查询对象可关闭?我在文档中没有找到任何有关它们打开了哪些资源的信息。SonarQube是正确的,还是可以安全地忽略这个警告?

谢谢。

英文:

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.

答案1

得分: 2

一个 JDBI 查询是对 JDBC Statement 和 ResultSet 的封装,其中 ResultSet 又包装了数据库游标。关闭查询会关闭 Statement 和 ResultSet,从而释放游标,释放数据库资源。数据库同时可以打开的游标数量是有限的。关闭查询不会关闭数据库连接。

如果 Sonarqube 知道关于 JDBI 的特定信息,我会感到惊讶,似乎它只是在遵循一个规则,即任何 AutoCloseable 的内容都应该被关闭。即便如此,对于大多数情况来说,这也是一个不错的规则。我会听取 Sonarqube 在这件事上的建议并关闭这些查询。

关闭连接应该会清理所有相关的游标,但最好在你使用完它们后立即释放,因为这能让数据库服务器更高效地工作。

英文:

A JDBI query is a wrapper around a JDBC Statement and a ResultSet, where the ResultSet in turn wraps a database cursor. Closing the query closes the Statement and the ResultSet, which de-allocates the cursor, freeing up database resources. There is a limit on how many cursors a database can have open at a time. Closing the query won’t close the database connection.

I would be surprised if Sonarqube knew anything specific about JDBI, it seems likely it is just following a rule saying anything AutoCloseable should get closed. Even so, that is not a bad rule to follow for most things. I would listen to Sonarqube on this one and close the queries.

Closing the connection should clean up all the related cursors for you, but it would be better to let them go as soon as you are done with them, because that lets the database server work more efficiently.

huangapple
  • 本文由 发表于 2020年9月29日 00:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64106022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定