Why not just eval{} it inside the END{} block to keep it from dying even if
you disconnected. You can also use a better method of turning off AutCommit
and then transactions are not commited until the final commit statement,
therefore if it dies anywhere in the middle of the DBI procedures, it will
not commit.
Ilya
-----Original Message-----
From: Karger, Amir
To: DBI Perl mailing List (E-mail)
Sent: 7/30/01 1:20 PM
Subject: Cleaning up via END{}
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