define a standard exception handler to handle database query faults

Discussion in 'Perl Misc' started by Filippo, Feb 15, 2007.

  1. Filippo

    Filippo Guest

    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
     
    Filippo, Feb 15, 2007
    #1
    1. Advertising

  2. Filippo

    tfe Guest

    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" <> 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_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
     
    tfe, Feb 15, 2007
    #2
    1. Advertising

  3. Filippo

    Peter Scott Guest

    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.53/DBI.pm#HandleError

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
     
    Peter Scott, Feb 15, 2007
    #3
  4. Filippo

    gf Guest

    On Feb 15, 1:15 am, "Filippo" <> 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_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.
     
    gf, Feb 15, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learner
    Replies:
    1
    Views:
    990
    Marina Levit [MVP]
    Jan 30, 2006
  2. shruds
    Replies:
    1
    Views:
    834
    John C. Bollinger
    Jan 27, 2006
  3. Todd A. Anderson
    Replies:
    1
    Views:
    605
    Jack Klein
    Nov 3, 2004
  4. theotyflos
    Replies:
    3
    Views:
    474
    Thomas Matthews
    Feb 19, 2004
  5. George2
    Replies:
    15
    Views:
    898
    Alf P. Steinbach
    Jan 29, 2008
Loading...

Share This Page