Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (19)
    As of 2014-12-18 14:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (56 votes), past polls