perl dbi rollback不工作

alp*_*alp 2 sql perl execute rollback 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();  
 }  
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*ens 7

根据数据库的不同,您可能需要在开始更改之前发出开始工作.我似乎记得Informix需要一个.

此外,看起来您在每次执行后发出提交或回滚.提交后,您无法回滚.通常有一个人说的话

$dbh->begin_work;
eval {
    for my $id (@asset_group) {  
        next if ($id eq '');  
        $sth->execute($id, $vars{other_id});  
    }
    1; #if it doesn't die then this will force it to return true
} or do {
    my $error = DBI->errstr;
    $dbh->rollback();
    die "could not insert rows: $error\n"
};
$dbh->commit();
Run Code Online (Sandbox Code Playgroud)

注意我怎么不用$@. $@不值得信任的.