perl dbi 回滚不起作用
我正在使用这种方法。如果 sql 中有错误,则仅对 asset_group 的第一个 id 进行回滚。其余 id 将被忽略。我的做法正确吗?
my $sql = "sql batch that update and insert depending on the condition";
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 0;
$dbh->{AutoCommit} = 0;
my $sth = $dbh->prepare($sql);
my @error = ();
my $num = 0;
foreach my $id (@asset_group) {
next if ($id eq '');
eval {
$sth->bind_param(1, $id);
$sth->bind_param(2, $vars{'other_id'});
$sth->execute();
};
if ($@) {
$dbh->rollback();
push @error, $@
} else {
$dbh->commit();
}
}
i am using this approach. If there is an error in the sql, rollback only happens for the first id of the asset_group. Rest of the ids are ignored. Am i doing it the right way?
my $sql = "sql batch that update and insert depending on the condition";
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 0;
$dbh->{AutoCommit} = 0;
my $sth = $dbh->prepare($sql);
my @error = ();
my $num = 0;
foreach my $id (@asset_group) {
next if ($id eq '');
eval {
$sth->bind_param(1, $id);
$sth->bind_param(2, $vars{'other_id'});
$sth->execute();
};
if ($@) {
$dbh->rollback();
push @error, $@
} else {
$dbh->commit();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据数据库的不同,您可能需要在开始更改之前发出开始工作 。我似乎记得 Informix 需要一个。
另外,看起来您在每次执行后都会发出提交或回滚。一旦提交,就无法回滚。 之类的内容
通常,人们会说“注意我如何不使用
$@
” 。$@
是不值得信任。Depending on the database, you may need to issue a begin work before you start changing things. I seem to remember Informix requiring one.
Also, it looks like you are issuing a commit or a rollback after each execute. Once you commit, you can't rollback. Normally one says something like
Note how I don't use
$@
.$@
is untrustworthy.