RE: Committing rolling back many objects at once?
[prev]
[thread]
[next]
[Date index for 2005/01/26]
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