Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deadlock Retries #34

Open
theory opened this issue Jun 23, 2013 · 2 comments
Open

Add Deadlock Retries #34

theory opened this issue Jun 23, 2013 · 2 comments

Comments

@theory
Copy link
Collaborator

theory commented Jun 23, 2013

Should work for serialization failures, too. See this blog post for details and discussion (especially the comments from Kevin Grittner).

@Perlover
Copy link
Contributor

+1
I very want to have fixup mode for deadlocks in InnoDB! :)

@matthewlenz
Copy link

How terrible is this (uses Try::Tiny::Retry)? The only obvious issue I see is that your code already reattempts connections for some of those exceptions so the total number of tries will be more.

sub retry_txn {
    my ($self, $code, $rethrow) = @_;

    my $dbc = $self->get_dbc();

    return retry {
        $dbc->txn(fixup => $code);
    } delay {
        return if $_[0] >= 10;
        sleep 0.5*$_[0];
    } on_retry {
        warn "Retrying transaction.";
    } retry_if {
        my $err = $_;
        $err = $err->error
            if (eval { $err->isa('DBIx::Connector::RollbackError') });
        $err =~ /deadlock|lost connection|gone away|can't connect/i;
    } catch {
        if ($rethrow) {
            my $err = $_;
            $err = $err->error
                if (eval { $err->isa('DBIx::Connector::RollbackError') });
            die $err; 
        }
    };
}

Usage would be something like:

$app->retry_txn(sub {
    $app->some_sql_actions();
});

or

try {
    $app->retry_txn(sub {
        $app->some_sql_actions();
    }, 1);
} catch {
    warn 'oh Snap!';
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants