Perl 5.24.0中使用词法变量重复连接DBI无效。

7
当我将perl环境从5.16.0切换到5.24.0时,我遇到了一个奇怪的行为,无法理解。此代码:
use DBI;

my $conn   = 'dbi:ODBC:sqlserver_xxxx';  
my $userid = 'dw_select';  
my $passwd = 'xxxx';

for ( 1 .. 100 ) {
    warn "start try $_";
    my $dbh = DBI->connect($conn, $userid, $passwd, { RaiseError => 1 } );
    warn "end try $_";  
}

在5.16.0版本中运行正常,但当切换到5.24.0版本时,我得到了以下结果:

start try 1 at test_con.pl line 9.
end try 1 at test_con.pl line 11.
start try 2 at test_con.pl line 9.
end try 2 at test_con.pl line 11.
start try 3 at test_con.pl line 9.
DBI connect('sqlserver_xxxx','dw_select',...) failed: 
 Unable to fetch information about the error at test_con.pl line 10.

通过这个修改,它再次可以无错误地运行:

use DBI;

my $conn   = 'dbi:ODBC:sqlserver_xxxx';  
my $userid = 'dw_select';  
my $passwd = 'xxxx';

my $dbh;    
for ( 1 .. 100 ) {
    warn "start try $_";
    $dbh = DBI->connect($conn, $userid, $passwd, { RaiseError => 1 } );
    warn "end try $_";  
}

有人能为此提供一个解释吗?


相关链接:http://www.nntp.perl.org/group/perl.dbi.users/2015/03/msg37124.html - nwellnhof
在 Perl v5.24 上运行 perl -MDBI -E"say $DBI::VERSION" 的结果是什么? - Borodin
2
不应该这样做,但是也许添加 $dbh->disconnect(); 会有帮助? - ikegami
1个回答

0

这是一个紧密的循环。我可能会尝试进行一些小的重写,看看是否有所帮助:

for ( 1 .. 100 ) {
   warn "start try $_";
   my $dbh = DBI->connect($conn, $userid, $passwd, { RaiseError => 1 } );
   if (!$dbh) {
       warn("Try $_ connect: " . $DBI::ERRSTR . "\n");
       last;
   }
   $dbh->disconnect;
}

ODBC接口可能无法在如此短的时间内处理单个线程中那么多句柄的实例化。希望显式断开连接会有所帮助。

干杯。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接