I have a single-thread Perl script running on a hosted shared server that mainly executes the following code:
my $O_dbh = DBI->connect("dbi:mysql:dbname=dbname", "abc", "xxx", {RaiseError => 1});
$O_dbh->begin_work();
my $O_sth1 = $O_dbh->prepare('SELECT COUNT(*) FROM mytable WHERE any = 5');
$O_sth1->execute();
my @result1 = $O_sth1->fetchrow_array();
my $oldValue = $result1[0];
$O_sth1->finish();
my $O_sth2 = $O_dbh->prepare('INSERT INTO mytable (any) VALUES (5)');
$O_sth2->execute();
$O_sth1->execute();
my @result2 = $O_sth1->fetchrow_array();
my $newValue = $result2[0];
if ($oldValue + 1 == $newValue) {
$O_dbh->commit();
}
else {
$O_dbh->rollback();
die "why?! new = $newValue, old = $oldValue";
}
Some times (<1%) the code runs into the rollback case and fails. On my local system I cannot reproduce this error. The database is MySQL 5.
CREATE TABLE `mytable` (
`id` int(11) NOT NULL auto_increment,
`any` int(11) NOT NULL default '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
How can I track down this error? Any help would be greatly appreciated.