关闭准备语句

扫码查看
本文介绍了关闭准备语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PreparedStatements和ResultSet在每次使用时都会创建一个新数据库实例?
或者换句话说,如果我使用PreparedStatement和ResultSet,我应该在每次使用后还是一旦结束后关闭它们?



例如:

  while(...){
p = connection.prepareStatement(...);
r = p.executeQuery();
while(r.next()){
....
}
}
//结束时关闭。或者还有任何其他p和r仍然打开...?
p.close();
r.close();

  while(...){
p = connection.prepareStatement(...);
r = p.executeQuery();
while(r.next()){
....
}
p.close();
r.close();
}

注意:当然我会使用try和close正确,这只是一个示例。

解决方案

您应该关闭每个打开的。当创建预准备语句或结果集时,数据库为这些资源分配资源,关闭它们会告诉数据库释放这些资源(很可能数据库会在超时时间之后重新分配这些资源,但调用close会让数据库知道它可以继续清理)。你的第二个例子是更好的,除非我关闭在准备语句之前的结果集。



因此,使用try块时,它将如下所示:

  while (...){
PreparedStatement p = connection.prepareStatement(...);
try {
ResultSet r = p.executeQuery();
try {
while(r.next()){
....
}
} finally {
try {
。关();
} catch(SQLException e){
//记录这个或某些东西 - 防止它们掩盖原始异常
}
}
}
finally {
try {
p.close();
} catch(SQLException e){
//记录这个或某些东西 - 防止它们掩盖原始异常
}
}
}

从关闭中捕获异常是丑陋的,但是如果在执行预准备语句期间抛出异常,结果集,你想确保你看到它,并且不是在关闭准备语句或结果集时抛出的异常(这是由于一些网络故障,你无法做任何事情)。



还要注意,使用try-with-resources将工作,除非如果你有一个case数据库操作成功,但调用close结果异常,那么异常将被抛出。 / p>

我建议人们使用spring-jdbc库(它处理关闭所有的东西),而不是手动启动iffy或verbose jdbc。


Does use of PreparedStatements and ResultSets creates a "new database instance" everytime they are used?Or, whith other words, if I use a PreparedStatement and a ResultSet, should I close them after every use or once I finish?

Example:

while (...){
        p = connection.prepareStatement(...);
        r = p.executeQuery();
        while (r.next()) {
        ....
        }
 }
 // We close at the end. Or there are any other p and r still opened...?
 p.close();
 r.close();

OR

while (...){
        p = connection.prepareStatement(...);
        r = p.executeQuery();
        while (r.next()) {
        ....
        }
        p.close();
        r.close();
 }

NOTE: Of course I would use try and close properly, this is just an example.

解决方案

You should close every one you open. When you create a prepared statement or result set the database allocates resources for those, and closing them tells the database to free those resources (it's likely the database will reallocate these resources eventually after a timeout period, but calling close lets the database know it can go ahead and clean up). Your second example is better, except I'd close the result set before the prepared statement.

So with try blocks included it would look like:

while (...){
        PreparedStatement p = connection.prepareStatement(...);
        try {
          ResultSet r = p.executeQuery();
          try {
            while (r.next()) {
              ....
            }
          } finally {
            try {
              r.close();
            } catch (SQLException e) {
            // log this or something -- prevent these from masking original exception
            }
          }
        }
        finally {
          try {
            p.close();
          } catch (SQLException e) {
            // log this or something -- prevent these from masking original exception
          }
        }
 }

Catching the exceptions from the close is ugly, but if you have an exception thrown during execution of the prepared statement, or during traversal of the result set, you want to make sure that you see it, and not an exception thrown when closing the prepared statement or result set (which is due to some network glitch you can't do anything about anyway).

Also be aware that using try-with-resources will work, except that if you have a case where the database operation succeeds but calling close results in an exception then the exception will get thrown.

I recommend people use the spring-jdbc library (which handles closing everything for you) instead of cranking out iffy or verbose jdbc by hand.

这篇关于关闭准备语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 11:57
查看更多