From a6f69c7dbe22920dff6fd8015bf440bdcf7479b6 Mon Sep 17 00:00:00 2001 From: Andreas Scherbaum Date: Sat, 15 Dec 2012 23:50:51 +0100 Subject: [PATCH] - reconnect database when connection failed - works as reliable as DBD::Pg::ping() works --- db.pm | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/db.pm b/db.pm index f95231b..fbaa0b5 100644 --- a/db.pm +++ b/db.pm @@ -260,14 +260,17 @@ sub verify_tables { sub ping { my $self = shift; - my $query = 'SELECT 1'; - if (!$self->do($query)) { - # ping failed, reconnect the database + if (!$self->{db_connection}->ping()) { + # ping failed + main::print_msg("Database connection failed, trying to reconnect", WARN); + # reconnect the database $self->reconnect_db_connection(); - if (!$self->do($query)) { + if (!$self->{db_connection}->ping()) { # reconnect failed + main::print_msg("Database reconnect failed", ERROR); return 0; } + main::print_msg("Database reconnected", INFO); } return 1; @@ -294,6 +297,11 @@ sub do { return 0; } + if (!$self->ping()) { + main::print_msg("Database connection failed", ERROR); + return 0; + } + my $result = $self->{db_connection}->do($query); if (!$result) { main::print_msg("Could not execute database query: $DBI::errstr", ERROR); @@ -329,6 +337,11 @@ sub query { return undef; } + if (!$self->ping()) { + main::print_msg("Database connection failed", ERROR); + return 0; + } + my $st = $self->{db_connection}->prepare($query); if (!$st->execute(@param)) { main::print_msg("Could not execute database query: $DBI::errstr", ERROR); -- 2.39.5