Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    transactions - reassurance please.

    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?

    Code:
     $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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    My reading of the DBI "Transactions" documentation is that
    Code:
    foo(...) # do lots of work here
    bar(...) # including inserts
    baz(...) # and updates
    are 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.

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thanks FishMonger.

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

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •