define a standard exception handler to handle database query faults

F

Filippo

hi,

my application is designed this way:

open a database connection at beginning

my $productionDatabase = DBI->connect(
"DBI:pgPP:dbname=$DATABASE_NAME;host=$DATABASE_SERVER;port=5432",
"postgres",
"postgres",
{
RaiseError => 1}
) or die (
"failed onnection failed: $DBI::errstr"
);


then keep open the connection and query database when needeed

my $sth = $dbh->prepare($sqlQuery);
$sth->execute;
my $result = $sth->fetchrow_arrayref;


I want that any database query fails (prepare, execute, fetchrow, do
etc) cause my application to die in case I don't want to manage the
error. As second step I want to cactch some errors in order to recover
from exception (ask the user to check the network connection and retry
the query...).

How can I do?

Thanks,

Filippo
 
T

tfe

Hi,

Yuo can use the errstr() function like that:
my $sth = $dbh->prepare($sqlQuery) or die ("ERROR ON SQL PREPARE ".
$sth->errstr);
$sth->execute or die ("ERROR ON EXECUTE ".$sth->errstr);
 
P

Peter Scott

my application is designed this way:

open a database connection at beginning

my $productionDatabase = DBI->connect( [...]
I want that any database query fails (prepare, execute, fetchrow, do
etc) cause my application to die in case I don't want to manage the
error. As second step I want to cactch some errors in order to recover
from exception (ask the user to check the network connection and retry
the query...).

http://search.cpan.org/~timb/DBI-1.53/DBI.pm#HandleError
 
G

gf

hi,

my application is designed this way:

open a database connection at beginning

my $productionDatabase = DBI->connect(
"DBI:pgPP:dbname=$DATABASE_NAME;host=$DATABASE_SERVER;port=5432",
"postgres",
"postgres",
{
RaiseError => 1}
) or die (
"failed onnection failed: $DBI::errstr"
);

then keep open the connection and query database when needeed

my $sth = $dbh->prepare($sqlQuery);
$sth->execute;
my $result = $sth->fetchrow_arrayref;

I want that any database query fails (prepare, execute, fetchrow, do
etc) cause my application to die in case I don't want to manage the
error. As second step I want to cactch some errors in order to recover
from exception (ask the user to check the network connection and retry
the query...).

The DBI allows us to pass default settings for PrintError, RaiseError,
AutoCommit. Those are your general fallback when you want the DBI to
handle problems.

If you want to change the default behavior, wrap the code in a {}
block, and use local to create a temporary overriding instance of the
variable, and set it to what you need it to be.

The DBI docs talk about temporarily overriding those behaviors.
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top