Commit 1ad18f51 authored by Leigh B. Stoller's avatar Leigh B. Stoller
Browse files

Another modification to my code to reconnect to the DB after a fork.

In this case, lets watch for a change to the current PID (process id)
and force a reconnect.
parent de5200bf
...@@ -273,6 +273,7 @@ else { ...@@ -273,6 +273,7 @@ else {
# to the DB in the child. # to the DB in the child.
# #
my @DB; my @DB;
my $DB_PID;
$DBQUERY_MAXTRIES = 1; $DBQUERY_MAXTRIES = 1;
$DBQUERY_RECONNECT = 1; $DBQUERY_RECONNECT = 1;
$DBCONN_MAXTRIES = 5; $DBCONN_MAXTRIES = 5;
...@@ -315,6 +316,7 @@ sub TBDBConnect($) ...@@ -315,6 +316,7 @@ sub TBDBConnect($)
} }
$DB[$dbnum]->{'dbh'}->{'PrintError'} = 0; $DB[$dbnum]->{'dbh'}->{'PrintError'} = 0;
$Mysql::QUIET = 1; $Mysql::QUIET = 1;
$DB_PID = $PID;
return 0; return 0;
} }
TBDBConnect(0); TBDBConnect(0);
...@@ -328,13 +330,32 @@ sub TBdbfork() ...@@ -328,13 +330,32 @@ sub TBdbfork()
} }
# New version. # New version.
sub TBDBReconnect() sub TBDBReconnect($)
{ {
my ($retry) = @_;
my ($exitonerr,$maxtries);
if ($retry) {
$exitonerr = $DBCONN_EXITONERR;
$DBCONN_EXITONERR = 0;
# And we want to keep trying for a long time!
$maxtries = $DBCONN_MAXTRIES;
$DBCONN_MAXTRIES = 10000;
}
for (my $i = 0; $i < @DB; $i++) { for (my $i = 0; $i < @DB; $i++) {
undef($DB[$i]); undef($DB[$i]);
return -1 return -1
if (TBDBConnect($i) != 0); if (TBDBConnect($i) != 0);
} }
if ($retry) {
$DBCONN_EXITONERR = $exitonerr;
$DBCONN_MAXTRIES = $maxtries;
}
#print "Reconnected to DB in process $PID\n";
return 0; return 0;
} }
...@@ -4531,6 +4552,16 @@ sub DBQueryN($$) ...@@ -4531,6 +4552,16 @@ sub DBQueryN($$)
$maxtries = 100000; $maxtries = 100000;
} }
# Reconnect to mysqld in child of fork.
if (defined($DB_PID) && $DB_PID != $PID) {
if (TBDBReconnect(1) != 0) {
$DBErrorString =
" Query: $query\n".
" Error: Could not reconnect to mysqld";
return undef;
}
}
while ($maxtries) { while ($maxtries) {
# Get this each time through the loop since we try reconnect below. # Get this each time through the loop since we try reconnect below.
my $db = $DB[$dbnum]; my $db = $DB[$dbnum];
...@@ -4554,21 +4585,11 @@ sub DBQueryN($$) ...@@ -4554,21 +4585,11 @@ sub DBQueryN($$)
# If we lose the connection to mysqld; lets try to reconnect. # If we lose the connection to mysqld; lets try to reconnect.
# #
if ($db->err == 2006 || $db->err == 2013) { if ($db->err == 2006 || $db->err == 2013) {
# This is really just for the mysqld watchdog daemon. # This is just for the mysqld watchdog daemon.
return undef return undef
if (! $DBQUERY_RECONNECT); if (! $DBQUERY_RECONNECT);
# Do not want to exit if this fails.
$DBCONN_EXITONERR = 0;
# And we want to keep trying for a long time!
$DBCONN_MAXTRIES = 1000;
my $reconnected = TBDBReconnect();
$DBCONN_EXITONERR = 1; if (TBDBReconnect(1) != 0) {
$DBCONN_MAXTRIES = 1;
if ($reconnected < 0) {
$DBErrorString = $DBErrorString =
" Query: $query\n". " Query: $query\n".
" Error: Could not reconnect to mysqld"; " Error: Could not reconnect to mysqld";
......
...@@ -60,7 +60,7 @@ if (!$debug) { ...@@ -60,7 +60,7 @@ if (!$debug) {
} }
} }
# Restablish connection in child to avoid transient startup error. # Restablish connection in child to avoid transient startup error.
libdb::TBDBReconnect(); libdb::TBDBReconnect(1);
# Be careful not to exit on transient error # Be careful not to exit on transient error
$libdb::DBQUERY_MAXTRIES = 0; $libdb::DBQUERY_MAXTRIES = 0;
...@@ -147,7 +147,8 @@ sub TryQuery() ...@@ -147,7 +147,8 @@ sub TryQuery()
$libdb::DBCONN_MAXTRIES = 2; $libdb::DBCONN_MAXTRIES = 2;
# Must always do this in the child. # Must always do this in the child.
libdb::TBDBReconnect(); exit(1)
if (libdb::TBDBReconnect(0) != 0);
if (!DBQueryWarn("show full processlist")) { if (!DBQueryWarn("show full processlist")) {
exit(1); exit(1);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment