# declare them here so # they are always in scope my ($dbh, $sth); eval { $dbh = DBI->connect(@connection_params, {RaiseError => 1, PrintError => 0, AutoCommit => 0}) || die "connection failed"; # die-ing in the above line might seem # redundant, since we are setting the # RaiseError flag, but you don't know # for sure that was set, so best to be # sure, since anything other than a valid # connection in $dbh is bad. $sth = $dbh->prepare($SQL); my $result = $sth->execute(@params); # ... do something with $result here ... # although it doesn't make sense to # commit for a SELECT statement, if # it wasn't a SELECT, i would commit # it here $dbh->commit(); }; # now lets check for an error/exception if ($@) { # check for "our" error if ($@ =~ /^connection failed$/) { print "Database unavailable: " . $DBI::errstr; } # otherwise something in DBI went wrong else { print $DBI::errstr; } # again, transactions are irrelvant # with SELECTs, but if you were to # need to rollback, you would do it # here. $dbh->rollback(); } # clean everything up now $sth->finish(); $dbh->disconnect();