English 中文(简体)
关闭开放的数据库链接?
原标题:Closing opened database connections?
  • 时间:2012-05-08 15:20:51
  •  标签:
  • java
  • jdbc

我的网络应用中有两个数据来源。 i 正在连接<代码>数据Source1.getConnection()。

my question is consider below code:

Connection connection = null;
connection = dataSource1.getConnection();
connection = dataSource2.getConnection();
connection.close();

我正在读到<条码>数据Source1链接,并立即分配<条码>数据Source2链接到链接变量。

As I am closing dataSource2 connection, does dataSource1 connection remains open? Or do I need to do as below?

Connection connection = null;
connection = dataSource1.getConnection();
connection.close();
connection = dataSource2.getConnection();
connection.close();
最佳回答

第二个例子是正确的,第一个例子将留下公开的联系。 此外,你应始终在<条码>范围内关闭资源(此处为连接),例如:

Connection connection = null;

try {
    connection = dataSource1.getConnection();
} catch (SQLException e) {
    // handle exception
} finally {
    try {
        connection.close();
    } catch (SQLException e) {
        // handle exception
    }   
}

try {
    connection = dataSource2.getConnection();
    connection.close();
} catch (SQLException e) {
    // handle exception
} finally {
    try {
        connection.close();
    } catch (SQLException e) {
        // handle exception
    }
}
问题回答

你们需要做你们在你的第二代法典中所表明的那样。

In the first case, the Garbage Collector may clean up the mess for you (after an undefined amount of time) and close the connection that you ve lost reference to (provided the JDBC driver vendor implemented the appropriate logic in the finalize method of their Connection implementation), however it is considered extremely bad practice to rely on this to happen.

rel=“nofollow”Connection 课程包括::接口,其中界定了“在不再需要时必须关闭的资源”。 因此,连接物体的定义是,需要使用<代码>close()方法加以适当管理。

On the other hand, the Connection.close() method is documented as "releasing the Connection object s database and JDBC resources immediately instead of waiting for them to be automatically released". Which implies that somewhere the connection resources will be freed eventually automatically.

On balance, though, I d say that it s better to explicitly close any connections you no longer need.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签