Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   define a standard exception handler to handle database query faults (http://www.velocityreviews.com/forums/t901854-define-a-standard-exception-handler-to-handle-database-query-faults.html)

Filippo 02-15-2007 08:15 AM

define a standard exception handler to handle database query faults
 
hi,

my application is designed this way:

open a database connection at beginning

my $productionDatabase = DBI->connect(
"DBI:PgPP:dbname=$DATABASE_NAME;host=$DATABASE_SER VER;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


tfe 02-15-2007 10:19 AM

Re: define a standard exception handler to handle database query faults
 
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);

--
tfe
http://tfeserver.be


On 15 fév, 09:15, "Filippo" <filippo2...@virgilio.it> wrote:
> hi,
>
> my application is designed this way:
>
> open a database connection at beginning
>
> my $productionDatabase = DBI->connect(
> "DBI:PgPP:dbname=$DATABASE_NAME;host=$DATABASE_SER VER;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




Peter Scott 02-15-2007 02:01 PM

Re: define a standard exception handler to handle database query faults
 
On Thu, 15 Feb 2007 00:15:39 -0800, Filippo wrote:
> 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.5...pm#HandleError

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


gf 02-15-2007 04:03 PM

Re: define a standard exception handler to handle database query faults
 
On Feb 15, 1:15 am, "Filippo" <filippo2...@virgilio.it> wrote:
> hi,
>
> my application is designed this way:
>
> open a database connection at beginning
>
> my $productionDatabase = DBI->connect(
> "DBI:PgPP:dbname=$DATABASE_NAME;host=$DATABASE_SER VER;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.



All times are GMT. The time now is 07:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.