Matt Seargent had an excellent way to open and close transactions 
in the Example::DB::Default class
of the AnyDBD distribution. It saves you from the manual 
attribute-toggling tedium, you are engaging in below.

Since he was too humble/busy to make this a general CPAN module, 
I have done so. It is called DBIx.

The below would be written roughly as:
sub insert_rows {
    my $dbh = shift->{dbh};
    open_transaction $dbh;

    eval {
    ### INSERT
    }

    commit_transaction $dbh and return 0 unless $@;
    rollback_transaction $dbh and return 'ERROR';
}

begin_transaction $dbh;
  eval {

      ### Existing code here(SQL, prepare, execute, etc) to
      ### insert rows into 2 different tables
      insert_rows;

      ### I want to add these next 2 lines in to this eval
      $rc = $my_obj->insert; ### my_obj is an object of ABC
      die "my_obj insert error" if $rc eq 'ERROR';

      commit_transaction $dbh;
   };
if ($@) {
        rollback_transaction $dbh;
      # handle transaction error
}



On Friday, December 28, 2001, at 12:45 PM, Hardy Merrill wrote:
>
> I'd like to incorporate invoking that insert method into
> another existing transaction, which basically looks like
> this:
>
>
>   $dbh->{RaiseError} = 1;
>   $dbh->{AutoCommit} = 0;
>   eval {
>
>      ### Existing code here(SQL, prepare, execute, etc) to
>      ### insert rows into 2 different tables
>
>      ### I want to add these next 2 lines in to this eval
>      $rc = $my_obj->insert; ### my_obj is an object of ABC
>      die "my_obj insert error" if $rc eq 'ERROR';
>
>      $dbh->commit;
>   };
>   if ($@) {
>      ### do rollback and handle transaction error here
>   }
>
> -------------------------------------------------------------
>
> But it seems like it won't work - an eval in an eval - can
> someone just confirm for me that this won't work?
>
> TIA.
>
> --
> Hardy Merrill
> Mission Critical Linux, Inc.
> http://www.missioncriticallinux.com
>
>

Reply via email to