Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > define a standard exception handler to handle database query faults

Reply
Thread Tools

define a standard exception handler to handle database query faults

 
 
Filippo
Guest
Posts: n/a
 
      02-15-2007
hi,

my application is designed this way:

open a database connection at beginning

my $productionDatabase = DBI->connect(
"DBIgPP: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

 
Reply With Quote
 
 
 
 
tfe
Guest
Posts: n/a
 
      02-15-2007
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" <(E-Mail Removed)> wrote:
> hi,
>
> my application is designed this way:
>
> open a database connection at beginning
>
> my $productionDatabase = DBI->connect(
> "DBIgPP: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



 
Reply With Quote
 
 
 
 
Peter Scott
Guest
Posts: n/a
 
      02-15-2007
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/

 
Reply With Quote
 
gf
Guest
Posts: n/a
 
      02-15-2007
On Feb 15, 1:15 am, "Filippo" <(E-Mail Removed)> wrote:
> hi,
>
> my application is designed this way:
>
> open a database connection at beginning
>
> my $productionDatabase = DBI->connect(
> "DBIgPP: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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Handle C++ exception and structured exception together George2 C++ 15 01-29-2008 07:34 AM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Exception handler for STATUS_GUARD_PAGE exception. Todd A. Anderson C++ 1 11-03-2004 05:08 AM



Advertisments