RE: Committing rolling back many objects at once?

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

From: Addison, Mark
Subject: RE: Committing rolling back many objects at once?
Date: 15:22 on 26 Jan 2005
From: Peter Speltz Sent: 20 January 2005 18:15
<snip>
> That's a quick response :). thanks.  Ok. so I CAN create many=20
> and commit many
> at once or rollback many at once but just not individually.=20
> very cool. So to
> update my example : this should work:
>=20
> my $person =3D Person->create( ...);
> my $emp    =3D Employee->create(...);
> if ($error) {
>    MyModelBase->dbi_rollback;
> }
> else {
>    MyModelBase->dbi_commit;
> }
>=20
> 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 =3D Person->create( ...);
     $emp    =3D Employee->create(...); =09
 });

It does error trapping for you and automatically rolls back
if anthing fails (and re-throws the exception) or commits=20
otherwise.=20
The other thing I've found is it helps you keep the scope of
your transactions small and all in one place. Which is=20
(nearly) always a good idea!

        sub do_transaction {
                my $class =3D shift;
                my ( $code ) =3D @_;
                # 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 =3D $@;
                        eval { $class->dbi_rollback }; # might also die!
                        die $commit_error;
                }
        }

mark
--

This email (and any attachments) is intended solely for the individual(s) t=
o whom addressed.=20
It may contain confidential and/or legally privileged information.=20
Any statement or opinions therein are not necessarily those of ITN unless s=
pecifically stated.=20
Any unauthorised use, disclosure or copying is prohibited.=20
If you have received this email in error, please notify the sender and dele=
te it from your system.=20
Security and reliability of the e-mail and attachments are not guaranteed.=20
You must take full responsibility for virus checking.



Independent Television News Limited,=20

Registered No. 548648 England,

VAT Reg. No: GB 756 2995 81,=20

200 Gray's Inn Road, London WC1X 8XZ,

Telephone: 020 7833 3000.

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