Repeated DBI connect does not work with lexical variable in Perl 5.24.0
Asked Answered
P

1

7

When I switched my perl environment from 5.16.0 to 5.24.0 I got a strange behavior that I cannot understand. This code

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 $_";  
}

runs fine on 5.16.0 but when switched to 5.24.0 I got following result:

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.

with this modification it runs without errors again:

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 $_";  
}

Does anyone of you have an explanation for that?

Portage answered 30/3, 2017 at 8:38 Comment(3)
Related: nntp.perl.org/group/perl.dbi.users/2015/03/msg37124.htmlGiralda
What is the result of perl -MDBI -E"say $DBI::VERSION" on Perl v 5.24?Giacometti
Shouldn't have to, but maybe adding $dbh->disconnect(); will help?Sherronsherry
C
0

This a tight loop. I might try a little rewrite to see if that helps:

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;
}

The ODBC interface may not be handling the instantiation of that many handles in a single thread in so short a time well. With luck, the explicit disconnect will help.

Cheers.

Cashman answered 14/7, 2022 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.