RE: Committing rolling back many objects at once?

[prev] [thread] [next] [Date index for 2005/01/26]

From: Peter Speltz
Subject: RE: Committing rolling back many objects at once?
Date: 17:43 on 26 Jan 2005
thank you very much. This looks great. i  tried that a while back and found
that it would only rollback the last object created.  But i may have done it
wrong. I'm glad to hear you can rollback many objects.  I'll try again. 

--- "Addison, Mark" <Mark.Addison@xxx.xx.xx> wrote:

> From: Peter Speltz Sent: 20 January 2005 18:15
> <snip>
> > That's a quick response :). thanks.  Ok. so I CAN create many 
> > and commit many
> > at once or rollback many at once but just not individually. 
> > very cool. So to
> > update my example : this should work:
> > 
> > my $person = Person->create( ...);
> > my $emp    = Employee->create(...);
> > if ($error) {
> >    MyModelBase->dbi_rollback;
> > }
> > else {
> >    MyModelBase->dbi_commit;
> > }
> > 
> > That's very cool if that works. thanks
> 
> A very nice way to handle transactions is Dominic Mitchell's
> do_transaction method which you will find in the CDBI docs (also
> I've copied it in below). Once you have that in you base class
> you would do:
> 
>  my ($person,$emp);
>  MyModuleBase->do_transaction( sub {
>      $person = Person->create( ...);
>      $emp    = Employee->create(...); 	
>  });
> 
> It does error trapping for you and automatically rolls back
> if anthing fails (and re-throws the exception) or commits 
> otherwise. 
> The other thing I've found is it helps you keep the scope of
> your transactions small and all in one place. Which is 
> (nearly) always a good idea!
> 
>         sub do_transaction {
>                 my $class = shift;
>                 my ( $code ) = @_;
>                 # Turn off AutoCommit for this scope.
>                 # A commit will occur at the exit of this block
> automatically,
>                 # when the local AutoCommit goes out of scope.
>                 local $class->db_Main->{ AutoCommit };
> 
>                 # Execute the required code inside the transaction.
>                 eval { $code->() };
>                 if ( $@ ) {
>                         my $commit_error = $@;
>                         eval { $class->dbi_rollback }; # might also die!
>                         die $commit_error;
>                 }
>         }
> 
> mark
> --
> 
> This email (and any attachments) is intended solely for the individual(s) to
> whom addressed. 
> It may contain confidential and/or legally privileged information. 
> Any statement or opinions therein are not necessarily those of ITN unless
> specifically stated. 
> Any unauthorised use, disclosure or copying is prohibited. 
> If you have received this email in error, please notify the sender and delete
> it from your system. 
> Security and reliability of the e-mail and attachments are not guaranteed. 
> You must take full responsibility for virus checking.
> 
> 
> 
> Independent Television News Limited, 
> 
> Registered No. 548648 England,
> 
> VAT Reg. No: GB 756 2995 81, 
> 
> 200 Gray's Inn Road, London WC1X 8XZ,
> 
> Telephone: 020 7833 3000.
> 
> 


=====
pjs


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - Easier than ever with enhanced search. Learn more.
http://info.mail.yahoo.com/mail_250

Committing rolling back many objects at once?
Peter Speltz 18:03 on 20 Jan 2005

Re: Committing rolling back many objects at once?
Perrin Harkins 18:06 on 20 Jan 2005

Re: Committing rolling back many objects at once?
Peter Speltz 18:15 on 20 Jan 2005

Re: Committing rolling back many objects at once?
Perrin Harkins 04:03 on 21 Jan 2005

RE: Committing rolling back many objects at once?
Addison, Mark 15:22 on 26 Jan 2005

RE: Committing rolling back many objects at once?
Peter Speltz 17:43 on 26 Jan 2005

Generated at 12:39 on 05 Feb 2005 by mariachi v0.52