How can I avoid a Die being raised, the handling used does not help

D

daniel.crosby

the resulting output occassionaly produces ERROR DIE main:
Can't call method "prepare" on an undefined value at line 1365. - No
event sent!
How can I prevent a die from occurring setting PrintError=>0,
RaiseError=>0 does not seem to help.

local $SIG{ALRM} = sub { die "TIMEOUT: unable to connect
database $G_CONF{INSERT_DB} \n"};

alarm $G_CONF{TIMEOUT_INVDB};

$l_cstr="DBI:Oracle:$G_CONF{INSERT_DB}";

$l_h =
DBI->connect($l_cstr,$G_CONF{INSERT_USER},$G_CONF{INSERT_PASS}, {
PrintError=>0, RaiseError=>0, AutoCom
mit=>1 } );
&Logging (2, $I, "insert", "prepare $l_insert");
-> line 1365 is below prepare
$l_x=$l_h->prepare ($l_insert ) or &Error ("Prepare $l_insert
failed", "insert");
$l_x->execute or &Error ("Execute $l_insert failed", "insert");


$G_DBCON->disconnect or &Warn ( "Can not close config DB
$G_CONF{CONFIG_DB} - $DBI::errstr", "disconnect");
alarm 0;
};
 
A

anno4000

the resulting output occassionaly produces ERROR DIE main:
Can't call method "prepare" on an undefined value at line 1365. - No
event sent!
How can I prevent a die from occurring setting PrintError=>0,
RaiseError=>0 does not seem to help.

You can wrap the offending statement in an eval block, but really
you shouldn't call prepare() at all when the preceding connect
doesn't return a defined value.
local $SIG{ALRM} = sub { die "TIMEOUT: unable to connect
database $G_CONF{INSERT_DB} \n"};

alarm $G_CONF{TIMEOUT_INVDB};

$l_cstr="DBI:Oracle:$G_CONF{INSERT_DB}";

$l_h =
DBI->connect($l_cstr,$G_CONF{INSERT_USER},$G_CONF{INSERT_PASS}, {
PrintError=>0, RaiseError=>0, AutoCom
mit=>1 } );

Check the result. If $l_h isn't defined, you can't call the prepare()
method on it. Catching the error won't help, because the actual error
happened earlier in connect().
&Logging (2, $I, "insert", "prepare $l_insert");
-> line 1365 is below prepare
$l_x=$l_h->prepare ($l_insert ) or &Error ("Prepare $l_insert
failed", "insert");
$l_x->execute or &Error ("Execute $l_insert failed", "insert");


$G_DBCON->disconnect or &Warn ( "Can not close config DB
$G_CONF{CONFIG_DB} - $DBI::errstr", "disconnect");
alarm 0;
};

Anno
 
T

Tad McClellan

Can't call method "prepare" on an undefined value at line 1365. - No
How can I prevent a die from occurring
$l_h =
DBI->connect($l_cstr,$G_CONF{INSERT_USER},$G_CONF{INSERT_PASS}, {
PrintError=>0, RaiseError=>0, AutoCom
mit=>1 } );


One way would be to check the return value:

do_something_other_than_die() unless defined $l_h;

&Logging (2, $I, "insert", "prepare $l_insert");
-> line 1365 is below prepare
$l_x=$l_h->prepare ($l_insert ) or &Error ("Prepare $l_insert
failed", "insert");


Another way would be to wrap it in a eval block (and you shouldn't
use ampersand on function calls unless you really do want the
semantics that it brings, see perlsub.pod):

eval {
$l_x = $l_h->prepare($l_insert) or
Error("Prepare $l_insert failed", 'insert');
}
if ( $@ ) {
do_something_other_than_die();
}
 
P

Paul Lalli

the resulting output occassionaly produces ERROR DIE main:
Can't call method "prepare" on an undefined value at line 1365. - No
event sent!
How can I prevent a die from occurring setting PrintError=>0,
RaiseError=>0 does not seem to help.

That's because you have a Perl error, not a Database error.
local $SIG{ALRM} = sub { die "TIMEOUT: unable to connect
database $G_CONF{INSERT_DB} \n"};

alarm $G_CONF{TIMEOUT_INVDB};

$l_cstr="DBI:Oracle:$G_CONF{INSERT_DB}";

$l_h =
DBI->connect($l_cstr,$G_CONF{INSERT_USER},$G_CONF{INSERT_PASS}, {
PrintError=>0, RaiseError=>0, AutoCom
mit=>1 } );

You shot yourself in the foot, by setting RaiseError to 0 here. If
you'd set it to 1, Perl would have died when this call to DBI->connect
failed. Because you set it to 0, and didn't bother checking the return
value, you have no way of knowing that the connect() failed until you
later try to erroneously use the return value of the failed connect().
&Logging (2, $I, "insert", "prepare $l_insert");
-> line 1365 is below prepare
$l_x=$l_h->prepare ($l_insert ) or &Error ("Prepare $l_insert
failed", "insert");

Here. When DBI->connect() failes, it returned undef to $l_h, but you
didn't check that possibility. So when you use it here, you're trying
to call a method on an undefined value, which is a Perl error.

Paul Lalli
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top