对非结果集结果查询重用语句

时间:2022-03-07 01:35:54

while it has been discussed many times, to close the database resources in shortest scope possible. I would like to know if its okay to reuse the statement objects, which does not return any result set. forexample a code like this,

虽然已经讨论了很多次,但是尽可能在最短的范围内关闭数据库资源。我想知道是否可以重用语句对象,它不返回任何结果。例如这样的代码,

 Statement st = con.getStatement();
 st.execute(TABLE1_DELETE_Query);
 st.execute(TABLE2_DELETE_Query);
 st.close();

I looked at similar questions on SF, yet I was unable to find that could have answered mine. If it has been answered, kindly just mention the reference.

我在科幻小说中看到了类似的问题,但我没能找到答案。如果已得到答复,请提及参考资料。

Thanks

谢谢

1 个解决方案

#1


3  

The lifetime of Statements should be kept short term, but re-using them is perfectly fine.

语句的生命周期应该保持在短期内,但是重复使用它们是完全没问题的。

A single unclosed ResultSet can cause an application to hang, but you can have 100s of unclosed statements without a problem. Typically a statement will be holding on to a very small piece of memory associated with the database connection and little else, but it can hold multiple result sets as could be returned by the getMoreResults() method, warnings for the getWarnings() method.

一个单独的unclosed ResultSet可以导致应用程序挂起,但是您可以有100多个没有问题的未关闭语句。通常,语句会占用与数据库连接相关的非常小的内存块,除此之外几乎没有其他东西,但是它可以保存多个结果集,getMoreResults()方法即getWarnings()方法的警告可以返回这些结果集。

Reusing a Statement many times within a single method and then closing it is good practice as it saves needlessly creating extra Statements. Creating a Statement and hanging on to it just in case it is wanted is bad practice.

在一个方法中多次重复使用一个语句,然后关闭它,这是很好的做法,因为这样可以避免不必要地创建额外的语句。创建一个声明,并紧紧抓住它,以防它被需要,这是不好的做法。

PreparedStatements are intended to be re-used, but it is still best to keep them within the scope of a single method, as that guarantees they have a short term life-span.

PreparedStatements希望被重用,但是最好将它们放在单个方法的范围内,因为这样可以保证它们有一个短期的生命周期。

#1


3  

The lifetime of Statements should be kept short term, but re-using them is perfectly fine.

语句的生命周期应该保持在短期内,但是重复使用它们是完全没问题的。

A single unclosed ResultSet can cause an application to hang, but you can have 100s of unclosed statements without a problem. Typically a statement will be holding on to a very small piece of memory associated with the database connection and little else, but it can hold multiple result sets as could be returned by the getMoreResults() method, warnings for the getWarnings() method.

一个单独的unclosed ResultSet可以导致应用程序挂起,但是您可以有100多个没有问题的未关闭语句。通常,语句会占用与数据库连接相关的非常小的内存块,除此之外几乎没有其他东西,但是它可以保存多个结果集,getMoreResults()方法即getWarnings()方法的警告可以返回这些结果集。

Reusing a Statement many times within a single method and then closing it is good practice as it saves needlessly creating extra Statements. Creating a Statement and hanging on to it just in case it is wanted is bad practice.

在一个方法中多次重复使用一个语句,然后关闭它,这是很好的做法,因为这样可以避免不必要地创建额外的语句。创建一个声明,并紧紧抓住它,以防它被需要,这是不好的做法。

PreparedStatements are intended to be re-used, but it is still best to keep them within the scope of a single method, as that guarantees they have a short term life-span.

PreparedStatements希望被重用,但是最好将它们放在单个方法的范围内,因为这样可以保证它们有一个短期的生命周期。

相关文章