Nested transactions - thoughts, suggestions and comments requested.
Microsoft's SQL-Server supports nested transactions something that seem "natural" to Perl's notion of modules. Yet many databases do not support nested transactions - MySQL comes to mind. Perhaps the very nature of my being somewhat of a "clueless newbie" incited me to wonder if it's feasible to implement what amounts to nested transactions for those databases that don't support it.
I've been experimenting with "code" that looks like
### Transaction starts my $dbh=DBI->connect(...); transactionalize { # Code to be run as a transaction here #- no begin_work, commit, rollback, START TRAN, COMMIT TRAN, ROL +LBACK TRAN ... } using ($dbh); ### Transaction ends
and implemented transactionalize/using (along the lines of Try::Tiny's try/catch) with
{ # This code implement the transactionalize { ... } using ( dbh ); my (@dbh_AO,$dbh_HRef,$error_E); sub transactionalize(&;$) { my ($body_CRef,$dbh_CRef)=@_; eval { ### Setting dbh_O ... push(@dbh_AO,(defined $dbh_CRef) ? $dbh_CRef->() : $Defa +ultdbh_O); local $dbh_AO[-1]->{RaiseError}=1; warn "@dbh_AO "; ### BEGIN TRANSACTION ... unless (exists $dbh_HRef->{$dbh_AO[-1]}) { $dbh_AO[-1]->begin_work; $dbh_HRef->{$dbh_AO[-1]}=$dbh_AO[-1]; }; ### Do the stuff here ... $body_CRef->(); ### COMMIT TRANSACTION ... if (@dbh_AO == 1) { for my $dbh_s (keys %$dbh_HRef) { ### $dbh_s $dbh_HRef->{$dbh_s}->commit(); }; }; pop(@dbh_AO); }; if ($@) { $error_E||=$@; ### ROLLBACK TRANSACTION ... if (@dbh_AO == 1) { for my $dbh_s (keys %$dbh_HRef) { ### $dbh_s eval { $dbh_HRef->{$dbh_s}->rollback(); }; }; }; pop(@dbh_AO); Carp::confess $error_E; }; } # transactionalize {}: sub using($) { my $dbh_O=shift; ### <where>: $dbh_O return sub { return $dbh_O; }; }; };
Initial trials give me hope. Much more testing remains.
There are no doubt bugs that remain and enhancements that are needed.
Thoughts, suggestions, comments appreciated.
added: RaiseError
In reply to Nested (sql) transactions by clueless newbie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |