Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Re: DBI perpare_cached and statment handle gone bad

by leriksen (Curate)
on Jul 09, 2003 at 02:15 UTC ( #272543=note: print w/replies, xml ) Need Help??


in reply to Re: Re: DBI perpare_cached and statment handle gone bad
in thread DBI perpare_cached and statment handle gone bad

Actually one could use eval to emulate a try-catch scenario (wuch is really one of the main uses of eval)
eval { # try $sth->execute(); #throw }; if ($@) { #catch if (<error text indicates a recoverable statement handle error) { ...delete cached statement handle, re-prepare SQL and re-execute... } }

Replies are listed 'Best First'.
Re4: DBI perpare_cached and statment handle gone bad
by dragonchild (Archbishop) on Jul 09, 2003 at 12:59 UTC
    What I'm doing is a step beyond that. I'm using Error (which implements a try-catch type system) and have the following code:
    my $dbh = try { DBI->connect( $conn_string, $props{USER}, $props{PWD}, { PrintError => 0, RaiseError => 1, }, ) } otherwise { throw Error::Database -text => ( $DBI::errstr || $@ || "Unknown Error: '$conn_string'" ); }; $dbh->{HandleError} = sub { throw Error::Database -text => $DBI::er +rstr }; return $dbh;
    I've defined Error::Database as such:
    package Error::Database; ###################################################################### +########## use 5.6.0; use strict; use warnings; ###################################################################### +########## # This is a baseclass I use for extending the standard Error class in +ways that don't matter here. use Error::Base; our @ISA = qw(Error::Base); ###################################################################### +########## sub new { my $class = shift; my $self = $class->SUPER::new(@_) || return undef; my $txt = $self->text; if ($txt =~ /text=([^=]+)./) { $self->{-text} = $1; } elsif ($txt =~ /Message String: (.*?)$/) { $self->{-text} = $1; } return $self; } 1;
    The reason for the regexes is I wanted to strip off meaningless guck to me and only giv eme the good stuff.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://272543]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2019-12-12 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?