4

我在使用 DBI 和 FreeTDS(在 Ubuntu 上)将一些数据插入 SQL Azure 时遇到了 Perl 的间歇性问题。可能会出现问题并且忽略某些行,然后我可以再次运行它而不会出现问题。

脚本:

print "Importing File $file: to Staging Table\n";
    open my $info, $file or die "Could not open $file: $!";

$dbh->do("EXEC DWTOOLS.InitStage;") or die $DBI::errstr ; #truncates the table

    my $sth = $dbh->prepare("INSERT INTO DWSTAGE.CDRImport (LINE) VALUES(?);") or die $DBI::errstr ;
    my $counter = 0;
    while( my $line = <$info>)  {
            $line =~ s/\r?\n$//;
            $counter++;
    print "Loading line $counter: $line\n" ;
            my $rc = $sth->execute($line) or die $DBI::errstr ;
            print "Result: $rc\n";
    }
    close $info;


print "\nChecking Data Warehouse: $counter lines expected\n" ;
my $checksth = $dbh->prepare("EXEC DWTOOLS.CheckStage ?;") or die $DBI::errstr ;
    my $checkrc = $checksth->execute($counter) or die $DBI::errstr ;

my @row;
while ( @row = $checksth->fetchrow_array(  ) ) {
    print "Row: @row\n";
}

给出输出:

Importing File filename: to Staging Table
Loading line 1: data redacted
Result: 1
Loading line 2: data redacted
Result: 1
etc. etc. with no indication of errors
Loading line 165: data redacted
Result: 1
Loading line 166: data redacted
Result: 1

Checking Data Warehouse: 166 lines expected
Row: 35 166
Row: 35 166
Loading to Data Warehouse

因此,当我查看表格时,它显示所有开始的行在它开始工作时一直丢失到某个点 - 可靠 - 直到结束 - 所以基本上,文件的最后 35 行被加载并且它们从 1 开始上升到 35,其中第 1 行实际上是第 (166-35+1) 行或其他。Azure 中的表有一个 PK 集群 IDENTITY 列,它从 1 开始并且没有间隙,所以就像第一个这么多插入已被删除而没有任何错误指示。这发生在各种文件、各种大小和文件中的各种位置。

文件被循环处理,每个文件都被打开、处理和关闭,以防与这种奇怪的行为有任何关系。该语句为每个文件重新准备一次,但如果这可能导致问题,SQL Azure 连接将在程序的整个生命周期内保持。如果连接失败,我仍然希望程序会死掉,但从执行返回的错误代码的缺乏来看,我不确定我会得到任何错误指示。

如果我继续并重新运行程序,所有的行都进来了,一切都很好。

我不确定要得出什么结论。现在,我的结论是 FreeTDS 有缺陷且不可靠。

4

1 回答 1

0

看起来数据库在截断过程完成之前正在执行插入。如果这是一个存储过程,您可以添加某种标志它已完成并在继续之前检查它。或者最好还是将整个数据库调用集合到一个事务中。

于 2015-10-04T19:06:35.777 回答