tags:

views:

990

answers:

4

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?

A: 

You could select null from dual.

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.

I'm hung up on the connect. I have to connect before I can SELECT.
Ah... OK. Now I see. 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 <instance name> and hit enter. So then you have to figure out how to call that from PHP. IDK PHP.
+5  A: 

Try this (fill in your ip and port):

if ( @fsockopen($db_ip,$db_port ) ) {
    //connect to database
} else {
    // didn't work
}
Jack
Seems to work. Thank you very much.
A: 

Here is what I do in 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

WACM161
The question is specifically tagged PHP
TravisO
I dont think offering a code snippet from another language is a bad thing,
WACM161
+1  A: 

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.

TravisO
He said it got hung up on the connection.
OIS