跟踪Perl中的非确定性MySQL错误

跟踪Perl中的非确定性MySQL错误,第1张

概述我有一个在托管共享服务器上运行的单线程Perl脚本,主要执行以下代码: 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 myta 我有一个在托管共享服务器上运行的单线程Perl脚本,主要执行以下代码:

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";}

有时(<1%)代码进入回滚情况并失败.在我的本地系统上,我无法重现此错误.数据库是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;

如何追踪此错误?任何帮助将不胜感激.

解决方法 假设您的数据库使用默认设置运行,我会更惊讶您的SELECT返回两个不同的值.

The documentation说这个

If the transaction isolation level is REPEAtable READ (the default level),all consistent reads within the same transaction read the snapshot established by the first such read in that transaction. You can get a fresher snapshot for your querIEs by committing the current transaction and after that issuing new querIEs.

因此,如果默认的REPEAtable READ隔离级别生效,我希望所有查询都会返回与第一次查询时数据库状态一致的数据.

但是,听起来这听起来可能有所帮助

With READ COMMITTED isolation level,each consistent read within a transaction sets and reads its own fresh snapshot.

我想你应该试试

$O_dbh->do('SET SESSION TRANSACTION ISolATION LEVEL READ COMMITTED');

在连接之后立即查看是否能为您修复问题.

但是,您应确保在此事务之后断开数据库句柄或将其返回到先前的隔离级别.否则你将开始得到不一致的结果.

总结

以上是内存溢出为你收集整理的跟踪Perl中的非确定性MySQL错误全部内容,希望文章能够帮你解决跟踪Perl中的非确定性MySQL错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1236387.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存