Skip to content

Instantly share code, notes, and snippets.

@ichirin2501
Last active August 29, 2015 14:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ichirin2501/f4b22e50356890a52621 to your computer and use it in GitHub Desktop.
index走査順によるデッドロック検証
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.014;
use Try::Tiny;
use Parallel::ForkManager;
use DBI;
my $pm = new Parallel::ForkManager(10);
for my $num (1 .. 300) {
$pm->start and next;
my $dbh = DBI->connect("dbi:mysql:test:localhost", 'root', '');
my $statement;
try {
$dbh->begin_work or die $dbh->errstr;
my @code_ids = (100 .. 110);
my @token_ids = (190 .. 200);
my $sth;
if ($num % 2) {
$statement = "SELECT * FROM shadow_lock WHERE code_id IN(" . join(',', ('?') x scalar(@code_ids)) . ") FOR UPDATE";
$sth = $dbh->prepare($statement);
$sth->execute(@code_ids);
} else {
$statement = "SELECT * FROM shadow_lock WHERE token_id IN(" . join(',', ('?') x scalar(@token_ids)) . ") FOR UPDATE";
$sth = $dbh->prepare($statement);
$sth->execute(@token_ids);
}
if ($sth->errstr) {
die $sth->errstr;
}
$sth->finish;
$dbh->commit;
} catch {
my $err = $_;
chomp $err;
warn "[DBERR:$err], [SQL:$statement]";
} finally {
$dbh->disconnect;
};
$pm->finish;
}
$pm->wait_all_children;
__END__
$ perl gazo.pl
DBD::mysql::st execute failed: Deadlock found when trying to get lock; try restarting transaction at gazo.pl line 39.
[DBERR:Deadlock found when trying to get lock; try restarting transaction at gazo.pl line 43.], [SQL:SELECT * FROM shadow_lock WHERE token_id IN(?,?,?,?,?,?,?,?,?,?,?) FOR UPDATE] at gazo.pl line 57.
DBD::mysql::st execute failed: Deadlock found when trying to get lock; try restarting transaction at gazo.pl line 39.
[DBERR:Deadlock found when trying to get lock; try restarting transaction at gazo.pl line 43.], [SQL:SELECT * FROM shadow_lock WHERE token_id IN(?,?,?,?,?,?,?,?,?,?,?) FOR UPDATE] at gazo.pl line 57.
DBD::mysql::st execute failed: Deadlock found when trying to get lock; try restarting transaction at gazo.pl line 35.
[DBERR:Deadlock found when trying to get lock; try restarting transaction at gazo.pl line 43.], [SQL:SELECT * FROM shadow_lock WHERE code_id IN(?,?,?,?,?,?,?,?,?,?,?) FOR UPDATE] at gazo.pl line 57.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment