in reply to CDBI and multiple databases

This seems to be working... posting this in case it may help someone else with the same problem some day.

Handling the rollback at the app level, not the DB level, by rolling back each handle if anything fails.

It calls a function AllTables::handles that returns all handles to every CDBI class (this isn't part of CDBI, but it is easy to cache the handles when the classes are defined) -- rolling back every object is probably somewhat redundant and overkill, as handles are unique by database, not by table, but I think that redundancy does no harm.

sub do_transaction { my ($code) = @_; &transactions_on; eval { $code->() }; &rollback_and_die if $@; &commit; &transactions_off; } sub rollback_and_die { my $commit_error = $@; # save it as rollback could fail eval { $_->rollback foreach (AllTables::handles); }; &transactions_off; # just to be safe die $commit_error; # and fatal death } sub commit { eval { $_->commit foreach (AllTables::handles); }; &rollback_and_die if $@; # commit failed, so bail } sub transactions_on { $_->{AutoCommit} = 0 foreach (AllTables::handles); } sub transactions_off { $_->{AutoCommit} = 1 foreach (AllTables::handles); }

Replies are listed 'Best First'.
Re: Re: CDBI and multiple databases
by mpeppler (Vicar) on Mar 21, 2004 at 17:11 UTC
    You should really test this under heavy load, with lots of concurrent users, and killing the client apps at random intervals to make sure that your code really works, and that all transactions that should be committed or rolled back together are performed correctly.

    Michael