重复的DBI连接在Perl 5.24.0中不适用于词法变量

hpcdzsge  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(150)

当我把我的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 $_";  
}

你们谁能解释一下吗?

avwztpqn

avwztpqn1#

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

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接口可能无法在这么短的时间内在单个线程中很好地处理这么多句柄的示例化。幸运的话,显式断开连接会有所帮助。
干杯干杯干杯

相关问题