user39653
user39653

Reputation: 1405

How do I test for an Oracle connection

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?

Upvotes: 3

Views: 6783

Answers (4)

Mark Brady
Mark Brady

Reputation:

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.

Upvotes: 0

TravisO
TravisO

Reputation: 9550

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.

Upvotes: 1

WACM161
WACM161

Reputation: 1033

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

Upvotes: -2

Jack
Jack

Reputation: 21183

Try this (fill in your ip and port):

if ( @fsockopen($db_ip,$db_port ) ) {
    //connect to database
} else {
    // didn't work
}

Upvotes: 6

Related Questions