User talk:Happygiraffe
From ClassDBI
The code snippet you added to the Using transactions node doesn't solve the problem you stated. Indeed, the code isn't any different than the do_transactions() method that I wrote. --EdwardSabol 18:40, 8 December 2005 (GMT)
I admit to abbreviating the code (slightly). But it did appear to work for me this afternoon, when I tried it. This is the full code that I checked in:
sub do_transaction (&) {
# Because we're not a method, we don't get given $class first.
# However, using this package name is good enough for what we
# need.
my $class = __PACKAGE__;
my ( $code ) = @_;
my $dbh = $class->db_Main;
# Temporarily disable AutoCommit, to cause a transaction to start
# for the rest of this scope. DBI will automatically issue a commit
# when this flips back to its previous value.
local $dbh->{ AutoCommit };
my @retval = eval { $code->() };
if ( my $err = $@ ) {
# Ensure we go back to the database, since the state may have
# changed under our feet.
$class->clear_object_index;
# NB: Ensure we are using the same connection that the main
# transaction is in.
eval { $dbh->rollback() };
die $err;
}
return wantarray ? @retval : $retval[0];
}
I've adjusted it slightly in order to work as a function instead of a method (I now import it into the namespace where needed) because it leads to much more readable code. But other than that, this code Works For Me[tm]. --Happygiraffe 19:19, 8 December 2005 (GMT)
Again, how is this code any different?
--EdwardSabol 19:27, 8 December 2005 (GMT)
I don't think it is. But I fail to see how it doesn't solve the problem of the rollback warning. When I ran my test script under DBI_TRACE=1, I saw a reconnect after the error. So by using the same $dbh, all I'm doing is ensuring that the ROLLBACK gets sent down the same connection that's had the error, not the new connection that somehow got opened when I called db_Main() in the error block. This solved the problem completely for me.
--Happygiraffe 20:17, 8 December 2005 (GMT)
OK, I finally got it. The change from "$class->rollback" to "$dbh->rollback" was too subtle for my aging eyes, I guess. I missed it twice! Anyway, I think I'll integrate your change in the do_transaction() on Using transactions.
--EdwardSabol 02:10, 9 December 2005 (GMT)

