簡體   English   中英

Perl DBI回滾​​不起作用

[英]perl dbi rollback not working

我正在使用這種方法。 如果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();  
 }  
}

根據數據庫的不同,您可能需要在開始更改之前發出開始工作 我似乎記得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();

注意我如何不使用$@ $@不可信的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM