two attempts at DBI connect call

D

dn.perl

I have two tns-connection-strings with me. Only one of them is active
at any given time.
Same schema, usernames, passwords apply to both.

I would like to try to connect via string-1, and if it fails connect
via string-2. But the program just aborts when a dbi->connect call
fails. No second chance allowed.

Sample code:
my $dbh ;
$dbh = DBI->connect("dbi:Oracle:tns-string1 etc etc etc");
if (!$dbh ) {
$dbh = DBI->connect("dbi:Oracle:tns-string2 etc etc etc");
}
## die if both attempts fail
die $dbh->errstr if !$dbh ; ## I can live without this statement
return $dbh ;

I suspect there must be some internal variable set by default to make
a program exit if DBI->connect fails.
How can I make the above code work?

TIA.
 
M

Mart van de Wege

Tad McClellan said:
You can trap fatal errors in Perl with "eval BLOCK".
Yeah, but is that necessary in this case?

As long as the RaiseError attribute is not set, DBI->connect just
returns an error code.

Is RaiseError set by default these days? On my system it isn't (Debian
Sid, DBI 1.616, DBD::pg 2.17.2).

The following code continues executing after the failed connect:

----- BEGIN CODE -----
#!/usr/bin/perl

use strict;
use warnings;

use DBI;

my $dbh = DBI->connect('dbi:pg:dbname=nonexistant',undef,undef);
print "Status" . DBI::errstr ."\n";
----- END CODE -----

Mart
 
X

Xho Jingleheimerschmidt

I have two tns-connection-strings with me. Only one of them is active
at any given time.
Same schema, usernames, passwords apply to both.

I would like to try to connect via string-1, and if it fails connect
via string-2. But the program just aborts when a dbi->connect call
fails. No second chance allowed.

Sample code:
my $dbh ;
$dbh = DBI->connect("dbi:Oracle:tns-string1 etc etc etc");
if (!$dbh ) {
$dbh = DBI->connect("dbi:Oracle:tns-string2 etc etc etc");
}
## die if both attempts fail
die $dbh->errstr if !$dbh ; ## I can live without this statement
return $dbh ;

I suspect there must be some internal variable set by default to make
a program exit if DBI->connect fails.

No, I don't think so. Is there a RaiseError=1 hidden in your "etc"?
How can I make the above code work?

You could use eval {}, but it shouldn't be needed.


Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top