Lev Lvovsky wrote:
we're trying to simulate a database outage for an application which needs to be able to fail gracefully, and come back up when the DB does. These errors can also be seen when the mysql server is restarted (connections get killed then too).

Yes, in both cases you no longer have a connection.

How do we explicitly do this with Apache::DBI maintaining the persistent connection from the beginning?

Well, if you break the connection, it's broken.  There's nothing that
Apache::DBI or anything else can do about that.  What Apache::DBI does
is intercept your DBI->connect calls and return an already open handle
if possible, or open a new one if needed.

Here's an example of recovering from a lost connection.  This is totally
untested code, but maybe it will give you the idea.  This assumes that
RaiseError is on.

eval {
    my $dbh = DBI->connect(...);
    local $dbh->{AutoCommit} = 0;
    $dbh->do('UPDATE ...');
    $dbh->commit;
}
if ($@) {
    # wrap this in eval, since it will fail if the connection is gone
    eval { $dbh->rollback; }
    # replace the dead connection with a new one if necessary
    $dbh = DBI->connect(...);
}

The DBI->connect() call in the error handling block will get intercepted
by Apache::DBI, and will return the current open connection if it is
still able to ping the database.  If it isn't able to, it will open a
new connection.

- Perrin

Reply via email to