java 如何使用 isValid(0) 检查连接是否已打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12261283/
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
How to check the connection is already opened with isValid(0)
提问by testing
i created a connection object (java.sql.Connection).
我创建了一个连接对象(java.sql.Connection)。
can i use this code to check the connection is already opened.
我可以使用此代码来检查连接是否已打开。
if (connection != null && connection.isValid(0)) {
return connection;
}
and if there is any problem in this please help me.
如果这有任何问题,请帮助我。
回答by CesarKuehl
Be careful with isValid(0)
, it will test the connection without any timeout, so your application could hang at this point.
小心isValid(0)
,它将在没有任何超时的情况下测试连接,因此此时您的应用程序可能会挂起。
It's better to specify a value higher than 0 (in seconds).
最好指定一个大于 0(以秒为单位)的值。
回答by CloudyMarble
Why not use the isOpen()methode Or isClosed()in java.?sql.?Connection
为什么不在java.?sql.?Connection 中使用isOpen()方法或isClosed()
EDIT:
编辑:
The isValid()Checks internally if the connection isnt closed which would be better IMO as it checks if the connection isValid too.
该isValid()的检查内部连接是否心不是关闭它会更好,国际海事组织,因为它会检查连接的IsValid了。
回答by tibtof
Your code is fine. It's safer than using isClose()
method, because is also test if the connection is still valid.
你的代码没问题。它比使用isClose()
方法更安全,因为还要测试连接是否仍然有效。