So even though my scripts are paradigms of well-written code, they die
sometimes. When that happens, I want to clean up my DBI stuff nicely.
Generally, the way I do it is:

my $SUCCESS;
# Init stuff snipped
my $dbh = DBI->connect(...) or die $DBH::errstr;
# DBI stuff snipped
$dbh->disconnect or die $DBI::errstr;
# Cleanup stuff snipped
$SUCCESS = 1;

END{
    if (!defined $SUCCESS && defined $dbh) {
        $dbh->rollback;
        $dbh->disconnect;
    }
}

This mostly works, since I usually die in the "DBI stuff" section. Howveer,
I've realized that there are a couple of problems. $dbh->rollback will fail
if I die before connecting (in "Init stuff") OR if I die after disconnecting
(in "Cleanup stuff"). But I couldn't think of a way to tell if a handle is
connected to anything or not. Do I need to have a second "SUCCESS" variable
that says whether I'm connected or not? Or is there a DBI method I didn't
see in my perusal of the docs?

Amir Karger
Curagen Corporation 

Reply via email to