This way is fine. Eval is fina as well. It depends if you have
RaiseError on, then eval is needed.
In eval you can simpley do this...
while($@ && $try <= 3)
{
eval
{
$try++;
$self->{DBH} = DBI->connect(....., {RaiseError => 1, PrintError => 0});
}
Notice $try++ before the connect, because eval returns right after the
error. Also notice PrintError turned off, because you don't want DBI to
call warn and die when something happens.
Ilya
> -----Original Message-----
> From: Brad Phinney [mailto:[EMAIL PROTECTED]]
> Sent: Sunday, January 13, 2002 11:36 AM
> To: [EMAIL PROTECTED]
> Subject: Retrying DB Connections
>
>
>
> Occasionally, a connectino to known DB does not go through for some
> unknwn reason. What is the best way to "retry" this
> connection attempt.
> Currently, I have the following
>
> $success = 0;
>
> $self->{DSN} =
> "DBI:$self->{DRIVER}:database=$self->{DATABASE};host=$self->{S
> QLHOST};port=$self->{BIND_PORT}";
>
> while ((!($success)) && ($try <= 3))
> {
> $self->{DBH} = DBI->connect($self->{DSN},
> $self->{USER}, $self->{PASSWORD}, { RaiseError => 0,
> PrintError => 1});
>
> ### If I cannot connect, take a time out
> if ($DBI::err != 0) {sleep(20);}
> else
> {$success = 1;}
> $try++;
>
> }
>
>
> Will this work. Or should I put the connection attempt in an EVAL
> statement and check for $@ after the statement executes.
>
>
> --
> Brad Phinney
> Senior Technical Consultant, Sendmail Inc.
> E-Mail : [EMAIL PROTECTED]
> Phone : 718-275-4782
> Cell : 917-531-7577
>