...

View Full Version : transactions - reassurance please.



bazz
10-18-2009, 06:52 AM
Beginning to 'utilise' transactions and I need some reassurance please.

I read this in cpan.



$dbh->{AutoCommit} = 0; # enable transactions, if possible
$dbh->{RaiseError} = 1;
eval {
foo(...) # do lots of work here
bar(...) # including inserts
baz(...) # and updates
$dbh->commit; # commit the changes if we get this far
};
if ($@) {
warn "Transaction aborted because $@";
$dbh->rollback; # undo the incomplete changes
# add other application on-error-clean-up code here
}


What I need to be sure of is that the foo,bar,baz shown above, can be representing actual sub routines and not only insert, update, replace statements.

so, can it just work like this?



$dbh->{AutoCommit} = 0; # enable transactions, if possible
$dbh->{RaiseError} = 1;

eval{

sub_update();
sub_replaceinto();
sub_select();
sub_insert_ignore();
$dbh->commit; # commit the changes if we get this far
};
if ($@) {
warn "Transaction aborted because $@";
$dbh->rollback; # undo the incomplete changes
# add other application on-error-clean-up code here
}


I would appreciate very much, whatever advice you may give.

bazz

FishMonger
10-18-2009, 04:10 PM
My reading of the DBI "Transactions" documentation is that

foo(...) # do lots of work here
bar(...) # including inserts
baz(...) # and updatesare subroutine calls, so what you're wanting to do is exactly what the doc describes.

A key clue to this understanding is this section of the transactions documentation.

A major advantage of the eval approach is that the transaction will be properly rolled back if any code (not just DBI calls) in the inner application dies for any reason.

bazz
10-18-2009, 08:09 PM
Thanks FishMonger.

I just wanted to be sure that I was planning it correctly and that the transactions would do as intended. :thumbsup:

bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum