From 59184caaa9854094b744981c89770f9dee11fab4 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 2 Oct 2008 19:03:52 +0000 Subject: [PATCH] - scheduler: FCGI: use mysql_ping() to check that DB connection is still alive before handling a request. If not, try to reconnect. This will hopefully make things work better if MySQL goes down and up when using FCGI. svn path=/trunk/boinc/; revision=16112 --- checkin_notes | 11 +++++++++++ db/db_base.cpp | 6 ++++++ db/db_base.h | 5 +++-- sched/main.cpp | 19 +++++++++++++------ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/checkin_notes b/checkin_notes index a338254e63..93a454315d 100644 --- a/checkin_notes +++ b/checkin_notes @@ -7949,3 +7949,14 @@ Rom 2 Oct 2008 cURL/ + +David 2 Oct 2008 + - scheduler: FCGI: use mysql_ping() to check that DB connection + is still alive before handling a request. If not, try to reconnect. + This will hopefully make things work better if MySQL goes down and up + when using FCGI. + + db/ + db_base.cpp,h + sched/ + main.cpp diff --git a/db/db_base.cpp b/db/db_base.cpp index 18fc370906..fe327c2135 100644 --- a/db/db_base.cpp +++ b/db/db_base.cpp @@ -125,6 +125,12 @@ int DB_CONN::rollback_transaction() { return do_query("ROLLBACK"); } +int DB_CONN::ping() { + int retval = mysql_ping(mysql); + if (retval) return ERR_DB_CANT_CONNECT; + return 0; +} + DB_BASE::DB_BASE(const char *tn, DB_CONN* p) : db(p), table_name(tn) { } diff --git a/db/db_base.h b/db/db_base.h index a009dcac7c..7bd8db155c 100644 --- a/db/db_base.h +++ b/db/db_base.h @@ -75,11 +75,12 @@ public: int insert_id(); void print_error(const char*); const char* error_string(); - - MYSQL* mysql; + int ping(); int start_transaction(); int rollback_transaction(); int commit_transaction(); + + MYSQL* mysql; }; // Base for derived classes that can access the DB diff --git a/sched/main.cpp b/sched/main.cpp index 69ec6053d7..e1e4a2b141 100644 --- a/sched/main.cpp +++ b/sched/main.cpp @@ -167,15 +167,22 @@ static void send_message(const char* msg, int delay) { int open_database() { int retval; - if (db_opened) return 0; + if (db_opened) { + retval = boinc_db.ping(); + if (retval) { + log_messages.printf(MSG_CRITICAL, + "lost connection to database - trying to reconnect\n" + ); + } else { + return 0; + } + } retval = boinc_db.open( config.db_name, config.db_host, config.db_user, config.db_passwd ); if (retval) { - log_messages.printf(MSG_CRITICAL, - "can't open database\n" - ); + log_messages.printf(MSG_CRITICAL, "can't open database\n"); return retval; } db_opened = true; @@ -450,7 +457,7 @@ int main(int argc, char** argv) { #ifndef _USING_FCGI_ fout = fopen(req_path, "w"); #else - fout = FCGI::fopen(req_path,"w"); + fout = FCGI::fopen(req_path,"w"); #endif if (!fout) { log_messages.printf(MSG_CRITICAL, @@ -471,7 +478,7 @@ int main(int argc, char** argv) { #ifndef _USING_FCGI_ fin = fopen(req_path, "r"); #else - fin = FCGI::fopen(req_path,"r"); + fin = FCGI::fopen(req_path,"r"); #endif if (!fin) { log_messages.printf(MSG_CRITICAL,