index走査順によるデッドロック検証
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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