From 702bed8c132b9e105236975e87513f106be480f8 Mon Sep 17 00:00:00 2001 From: Ramon Novoa Date: Wed, 18 Oct 2017 10:45:55 +0200 Subject: [PATCH] Retry queries when a deadlock is found. Ref pandora_enterprise#1126. --- pandora_server/lib/PandoraFMS/DB.pm | 56 +++++++++++++++++++---------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index f7cb4e022f..4176a47d94 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -805,25 +805,20 @@ sub db_delete_limit ($$$$;@) { sub db_insert ($$$;@) { my ($dbh, $index, $query, @values) = @_; my $insert_id = undef; - - - # MySQL - if ($RDBMS eq 'mysql') { + + eval { $dbh->do($query, undef, @values); $insert_id = $dbh->{'mysql_insertid'}; - } - # PostgreSQL - elsif ($RDBMS eq 'postgresql') { - $insert_id = get_db_value ($dbh, $query . ' RETURNING ' . $RDBMS_QUOTE . $index . $RDBMS_QUOTE, @values); - } - # Oracle - elsif ($RDBMS eq 'oracle') { - my $sth = $dbh->prepare($query . ' RETURNING ' . $RDBMS_QUOTE . (uc ($index)) . $RDBMS_QUOTE . ' INTO ?'); - for (my $i = 0; $i <= $#values; $i++) { - $sth->bind_param ($i+1, $values[$i]); + }; + if ($@) { + my $exception = @_; + if ($DBI::err == 1213) { + $dbh->do($query, undef, @values); + $insert_id = $dbh->{'mysql_insertid'}; + } + else { + die($exception); } - $sth->bind_param_inout($#values + 2, \$insert_id, 99); - $sth->execute (); } return $insert_id; @@ -834,8 +829,20 @@ sub db_insert ($$$;@) { ########################################################################## sub db_update ($$;@) { my ($dbh, $query, @values) = @_; - - my $rows = $dbh->do($query, undef, @values); + my $rows; + + eval { + $rows = $dbh->do($query, undef, @values); + }; + if ($@) { + my $exception = @_; + if ($DBI::err == 1213) { + $rows = $dbh->do($query, undef, @values); + } + else { + die($exception); + } + } return $rows; } @@ -1016,7 +1023,18 @@ sub db_do ($$;@) { my ($dbh, $query, @values) = @_; #DBI->trace( 3, '/tmp/dbitrace.log' ); - $dbh->do($query, undef, @values); + eval { + $dbh->do($query, undef, @values); + }; + if ($@) { + my $exception = @_; + if ($DBI::err == 1213) { + $dbh->do($query, undef, @values); + } + else { + die($exception); + } + } } ########################################################################