faqts : Computers : Programming : Languages : Perl : webside heack : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

26 of 26 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I detect if the database is down?

Jan 27th, 2000 09:30
Nathan Wallace, Bill McKinnon


I've written code that uses DBI and is invoked once per request (which
for the app was potentially many times a second).  I handled the DB
being down by doing something like the following:

# Unless DB is up...
unless ( defined($dbh) and $dbh->ping() ) {   
    # See if the interval we wait before trying a reconnect is up yet
    if ( (time() - $last_error) > $RECONNECT_INTERVAL ) {
        if (<reconnect failed>) {
            <do whatever we should do when the db is     
             down...error...whatever>
            $last_error = time();
        }
    }
}
<do stuff we do when DB is up>

In my case the default thing to do if the db was down didn't make the
app completely useless, so my main concern was keeping the app up and
running and not bombing if anything happened to the database. Looking at
my code I just realized I am assuming that $dbh->ping() doesn't block 
forever...anyone know if this is true offhand?