如何测试 Oracle 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/389692/
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 do I test for an Oracle connection
提问by user39653
I'm trying to connect to an Oracle DB which is currently offline. When it's online it's not a problem, however, now that it's offline my program is getting hung up on the $connection = oci_connect() line and timing out. How do I simply check the connectio and bail out if it's not there?
我正在尝试连接到当前处于脱机状态的 Oracle 数据库。当它在线时不是问题,但是,现在它处于离线状态,我的程序在 $connection = oci_connect() 行上挂断并超时。我如何简单地检查连接并在它不存在时退出?
回答by Hyman
Try this (fill in your ip and port):
试试这个(填写你的ip和端口):
if ( @fsockopen($db_ip,$db_port ) ) {
//connect to database
} else {
// didn't work
}
回答by TravisO
This gives you both a manual error, plus will return the actual error.
这给你一个手动错误,加上将返回实际错误。
$connection = oci_connect() or die("Critical Error: Could not connect to database.\n\n". oci_error());
Make sure to test this as hopefully the Oracle error doesn't do something stupid like return the connection string (with your DB password) but I wouldn't assume until you see for yourself.
请务必对此进行测试,因为希望 Oracle 错误不会做一些愚蠢的事情,例如返回连接字符串(使用您的数据库密码),但在您亲眼看到之前我不会假设。
回答by TravisO
You could select null from dual.
您可以从双中选择 null。
OK, now I see what your asking, I think.
好的,现在我明白你在问什么了,我想。
You want to know how to tell if a database is up before you connect to it?
您想知道如何在连接之前判断数据库是否已启动?
You can use TNSPING to see if a database is up... ok, maybe that's not 100% accurate but it's a good indicator. go to a command prompt and type TNSPING and hit enter. So then you have to figure out how to call a command line tool from PHP.
您可以使用 TNSPING 来查看数据库是否已启动……好吧,也许这不是 100% 准确,但这是一个很好的指标。转到命令提示符并键入 TNSPING 并按 Enter。那么你必须弄清楚如何从 PHP 调用命令行工具。
回答by WACM161
Here is what I do in ASP.NET
这是我在 ASP.NET 中所做的
Dim OracleConn As New OracleConnection(YOUR CONNECTION STRING HERE)
Try
OracleConn.Open()
OracleConn.Close()
Catch ex As Exception
Session("ErrorMessage") = "OracleConn: " & ex.Message
Response.Redirect("AccessDenied.aspx")
End Try
It doesnt necessarily say the DB is offline, but an exception will occur if the connection cannot be opened
不一定说DB离线,但是打不开连接会出现异常