>>>>> "Tim" == Tim Bunce <[EMAIL PROTECTED]> writes:

Tim> But as I tinker with it I wonder if it's a solution chasing a problem.
Tim> Where, and how, and why, would it be used?

I used something like this in
http://www.stonehenge.com/merlyn/LinuxMag/col52.html, although it was
layered via Class::DBI and called "atomically".

It's useful when you have a pattern of "do all these things together,
or roll them back together".  That pattern comes up a lot, so having a
wrapper to add the external logic is nice.

  sub atomically {
    my $class = shift;
    my $action = shift;         # coderef
    local $class->db_Main->{AutoCommit}; # turn off AutoCommit for this block

    my @result;
    eval {
      @result = wantarray ? $action->() : scalar($action->());
      $class->dbi_commit;
    };
    if ($@) {
      warn "atomically got error: $@";
      my $commit_error = $@;
      eval { $class->dbi_rollback };
      die $commit_error;
    }
    die $@ if $@;
    wantarray ? @result : $result[0];
  }


-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Reply via email to