Created
September 18, 2011 19:51
-
-
Save alexdioso/1225479 to your computer and use it in GitHub Desktop.
Perl DBI HandleError
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $dbh = DBI->connect( | |
'dbi:Pg:dbname=DATABASE_NAME', | |
q{}, | |
q{}, | |
{ | |
RaiseError => 1, | |
AutoCommit => 0, | |
PrintError => 0, | |
ShowErrorStatement => 1, | |
HandleError => sub { | |
my $errormsg = shift; | |
my $handle = shift; | |
my $dbh; | |
my $type = $handle->{'Type'}; | |
if ($type eq 'st') { | |
$dbh = $handle->{'Database'}; | |
} elsif ($type eq 'db') { | |
$dbh = $handle; | |
} else { | |
warn "Unknown handle type: $type"; | |
confess $errormsg; | |
} | |
$dbh->rollback; | |
confess $errormsg; | |
} | |
} | |
); | |
# No check needed | |
$sth->execute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment