Author: spadkins
Date: Wed Oct  3 08:15:17 2007
New Revision: 10048

Modified:
   p5ee/trunk/App-Repository/lib/App/Repository/DBI.pm
   p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm

Log:

retry after deadlock on _do()

Modified: p5ee/trunk/App-Repository/lib/App/Repository/DBI.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/Repository/DBI.pm (original)
+++ p5ee/trunk/App-Repository/lib/App/Repository/DBI.pm Wed Oct  3 08:15:17 2007
@@ -185,7 +185,8 @@
             };
             if ($@) {
                 delete $self->{dbh};
-                if ($@ =~ /Lost connection/ || $@ =~ /server has gone away/) {
+                my $retryable_connection_error_regex = 
$self->retryable_connection_error_regex();
+                if ($@ =~ /$retryable_connection_error_regex/i) {
                     $self->{context}->log({level=>1},"DBI Exception (retrying) 
in _connect(): $@");
                     sleep(1);
                 }
@@ -206,6 +207,14 @@
     return(defined $self->{dbh});
 }
 
+sub retryable_connection_error_regex {
+    return "Lost connection|server has gone away";
+}
+
+sub retryable_modify_error_regex {
+    return "Lost connection|server has gone away|Deadlock found";
+}
+
 # likely overridden at the subclass level
 sub _dsn {
     &App::sub_entry if ($App::trace);
@@ -409,6 +418,7 @@
     if ($context_options->{explain_sql}) {
         $self->explain_sql($sql);
     }
+    ### TODO: make this similar to the _connect code, using a regex named 
retryable_select_error_regex
     while (1) {
         eval {
             $row = $dbh->selectrow_arrayref($sql);
@@ -2197,6 +2207,7 @@
     }
     if (defined $dbh) {
         eval {
+            ### TODO: make this work with regex for retry
             $retval = $dbh->do($sql, undef, @$row);
             $retval = 0 if ($retval == 0); # turn "0E0" into plain old "0"
         };
@@ -2250,6 +2261,7 @@
             }
             if (defined $dbh) {
                 eval {
+                    ### TODO: make this work with regex for retry
                     $retval = $dbh->do($sql, undef, @$row);
                     $retval = 0 if ($retval == 0); # turn "0E0" into plain old 
"0"
                 };
@@ -2295,6 +2307,7 @@
                 }
                 if (defined $dbh) {
                     eval {
+                        ### TODO: make this work with regex for retry
                         $retval = $dbh->do($sql, undef, @$row);
                         $retval = 0 if ($retval == 0); # turn "0E0" into plain 
old "0"
                     };
@@ -2352,6 +2365,7 @@
     my $dbh = $self->{dbh};
     if (defined $dbh) {
         eval {
+            ### TODO: make this work with regex for retry
             $retval = $dbh->do($sql);
             $retval = 0 if ($retval == 0); # turn "0E0" into plain old "0"
         };
@@ -2397,6 +2411,7 @@
     my $dbh = $self->{dbh};
     if (defined $dbh) {
         eval {
+            ### TODO: make this work with regex for retry
             $retval = $dbh->do($sql);
             $retval = 0 if ($retval == 0); # turn "0E0" into plain old "0"
         };
@@ -2438,6 +2453,7 @@
     my $dbh = $self->{dbh};
     if (defined $dbh) {
         eval {
+            ### TODO: make this work with regex for retry
             $retval = $dbh->do($sql);
             $retval = 0 if ($retval == 0); # turn "0E0" into plain old "0"
         };
@@ -2479,6 +2495,7 @@
     my $dbh = $self->{dbh};
     if (defined $dbh) {
         eval {
+            ### TODO: make this work with regex for retry
             $retval = $dbh->do($sql);
             $retval = 0 if ($retval == 0); # turn "0E0" into plain old "0"
         };
@@ -2520,18 +2537,37 @@
     }
     if (defined $dbh) {
         $self->{sql} = $sql;
-        eval {
-            if ($sql =~ /^select/i) {
-                $retval = $dbh->selectall_arrayref($sql);
+        my $continue = 1;
+        my $tries = 1;
+        while ($continue) {
+            eval {
+                if ($sql =~ /^select/i) {
+                    $retval = $dbh->selectall_arrayref($sql);
+                }
+                else {
+                    $retval = $dbh->do($sql);
+                    $retval = 0 if ($retval == 0); # turn "0E0" into plain old 
"0"
+                }
+            };
+            if ($@) {  # Log the error message with the SQL and rethrow the 
exception
+                my $retryable_modify_error_regex = 
$self->retryable_modify_error_regex();
+                if ($@ =~ /$retryable_modify_error_regex/i) {
+                    if ($tries >= 3) {
+                        $self->{context}->log({level=>1},"DBI Exception (fail) 
(tries=$tries) in _do(): [EMAIL PROTECTED]");
+                        die $@;
+                    }
+                    $self->{context}->log({level=>1},"DBI Exception (retry) 
(tries=$tries) in _do(): [EMAIL PROTECTED]");
+                    $tries++;
+                    sleep(1);
+                }
+                else {
+                    $self->{context}->log({level=>1},"DBI Exception (fail) in 
_do(): [EMAIL PROTECTED]");
+                    die $@;
+                }
             }
             else {
-                $retval = $dbh->do($sql);
-                $retval = 0 if ($retval == 0); # turn "0E0" into plain old "0"
+                $continue = 0;
             }
-        };
-        if ($@) {  # Log the error message with the SQL and rethrow the 
exception
-            $self->{context}->log({level=>1},"DBI Exception (fail) in _do(): 
[EMAIL PROTECTED]");
-            die $@;
         }
     }
     if ($debug_sql) {

Modified: p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm       (original)
+++ p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm       Wed Oct  3 
08:15:17 2007
@@ -313,6 +313,7 @@
                 print $App::DEBUG_FILE "DEBUG_SQL: _insert_rows()\n";
                 print $App::DEBUG_FILE $sql;
             }
+            ### TODO: make this work with regex for retry
             $retval = $dbh->do($sql);
             if ($debug_sql) {
                 print $App::DEBUG_FILE "DEBUG_SQL: retval [$retval] 
$DBI::errstr\n";
@@ -359,6 +360,7 @@
                 print $App::DEBUG_FILE "DEBUG_SQL: _insert_rows()\n";
                 print $App::DEBUG_FILE $sql;
             }
+            ### TODO: make this work with regex for retry
             $retval = $dbh->do($sql);
             if ($debug_sql) {
                 print $App::DEBUG_FILE "DEBUG_SQL: retval [$retval] 
$DBI::errstr\n";
@@ -420,6 +422,7 @@
             print $App::DEBUG_FILE "DEBUG_SQL: _insert_rows()\n";
             print $App::DEBUG_FILE $sql;
         }
+        ### TODO: make this work with regex for retry
         $retval = $dbh->do($sql);
         if ($debug_sql) {
             print $App::DEBUG_FILE "DEBUG_SQL: retval [$retval] 
$DBI::errstr\n";

Reply via email to