什么时候应该关闭Java PreparedStatement? [英] When should a java PreparedStatement be closed?
问题描述
在教程使用准备好的语句" 中指出他们应该总是关闭.假设我有一个功能
In the tutorial "Using Prepared Statements" it states that they should always be closed. Suppose I have a function
getPrice() {
}
我希望每秒被调用多次.此方法是否应该通过每个方法调用来打开和关闭PreparedStatement?这似乎有很多开销.
that I expect to be called multiple times per second. Should this method be opening and closing the PreparedStatement with every single method call? This seems like a lot of overhead.
推荐答案
首先,永远不会打开PreparedStatement
.只是执行的准备好的Statement
.该语句被发送到执行由PreparedStatement
编译的SQL语句的RDBMS.与SQL语句的连接应在SQL查询期间打开,并在不需要其他RDMS调用时关闭.
First of all, PreparedStatement
are never opened. It's just a prepared Statement
that is executed. The statement is sent to the RDBMS that executes the SQL statement compiled by the PreparedStatement
. The connection to the SQL statement should be opened during the duration of the SQL querying and closed when no other RDMS calls is needed.
您可以根据需要发送许多Statement
/PreparedStatement
,前提是您最终完成对它们的ResultSet
和PreparedStatement
的关闭后,再关闭RDBMS连接
You can send many Statement
/PreparedStatement
as you require provided that you finally close its ResultSet
and PreparedStatement
once you're completed with them and then close the RDBMS connection.
这篇关于什么时候应该关闭Java PreparedStatement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!