2009-08-05

 

[Perl] Waiting for a DB connection to become available

While working on Hadoop, I found that connecting to a DB to get or insert data presented a problem as you often need more connections then what's available. My solution right now is to "wait" for a connection to become available - without using any fancy queuing mechanisms:
# Connect to DB
my $cont = 0;
my $tries = 0;
my $dbh1;
while( not $cont ) {

$tries++;
eval {

$dbh1 = DBI->connect( "dbi:mysql:database=$dbdb;host=$dbhost;port=$dbport", "$dbuser", "$dbpass" );

};

if( $@ ) { sleep( 1 ); } else { $cont++; }

if( $tries > 3600 ) {

# We have been trying for an hour - give up
print "GIVE_UP\t1\n";
exit;

}

}


And that's it. When the while loop finally exists, you have a DB handle to use. In the above example we retry for more then an hour - which should be sufficient for most scenarios.

Hope some one else find it useful as well.

Labels: , , ,


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?