Java 以编程方式检查 JDBC 中的打开连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/274822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:22:04  来源:igfitidea点击:

programmatically checking for open connection in JDBC

javaoraclejdbc

提问by Chaitanya MSV

How do I check for an open connection in jdbc for oracle database?

如何在 jdbc 中检查 oracle 数据库的打开连接?

Note: conn.isClosed()cannot be used for this.

注意:conn.isClosed()不能用于此。

采纳答案by reallyinsane

Usually a Connection Pool will also use the Connection.isClosed() method to check if the Connection is still valid. The problem is that not all JDBC drivers will handle this call correctly. So I assume that there are some simple check statements just like RealHowTo said. For Oracle he already mentioned the "SELECT 1 FROM Dual" which should succeed always for Oracle databases. I think that there are similar queries for the different database. I can remember that in a previous project we also implemented an own Connection Pool which used such validation queries.

通常连接池也会使用 Connection.isClosed() 方法来检查连接是否仍然有效。问题是并非所有 JDBC 驱动程序都能正确处理此调用。所以我假设有一些简单的检查语句,就像 RealHowTo 所说的那样。对于 Oracle,他已经提到了“SELECT 1 FROM Dual”,它应该总是适用于 Oracle 数据库。我认为对不同的数据库有类似的查询。我记得在之前的一个项目中,我们还实现了一个自己的连接池,它使用了这样的验证查询。

回答by RealHowTo

Something like:

就像是:

Statement stmt = null;
ResultSet rs =null;
try {
   stmt = conn.createStatement();
   // oracle
   rs = stmt.executeQuery("SELECT 1 FROM Dual");
   // others
   // rs = stmt.executeQuery("SELECT 1");
   if (rs.next())
      return true; // connection is valid
}
catch (SQLException e) {
   // TODO : log the exception ...
   return false;
}
finally {
   if (stmt != null) stmt.close();
   if (rs != null) rs.close();
} 

Note that if the connection is coming from a Connection Pool (in a Application Server for example) then the Pool may have a mechanism to check if a connection is valid or not. With BEA, you specify the SELECT in the "test-on-reserve" property.

请注意,如果连接来自连接池(例如在应用程序服务器中),则该池可能具有检查连接是否有效的机制。使用 BEA,您可以在“test-on-reserve”属性中指定 SELECT。

If you are developing your own pool then you may want to take a look at how others are doing it (ex. Proxool).

如果您正在开发自己的池,那么您可能想看看其他人是如何做的(例如Proxool)。

回答by Nicholas

See thisposting.

看到这个帖子。

The referenced solutions are similar to the one posted here (quick query against DUAL to validate) but there is also an interesting solution provided by JBoss specific to Oracle using the proprietary PING method in the Oracle JDBC Connection class. See the code here.

引用的解决方案类似于此处发布的解决方案(针对 DUAL 的快速查询以进行验证),但 JBoss 还使用 Oracle JDBC Connection 类中的专有 PING 方法为 Oracle 提供了一个有趣的解决方案。请参阅此处的代码。

//Nicholas

//尼古拉斯

回答by J?rgen

Use pingDatabase(int timeout)Implemented in OracleConnection since 9.0.1

使用pingDatabase(int timeout)从 9.0.1 开始在 OracleConnection 中实现