[Dbix-class] RFC: DBIx-Class-OptimisticLocking

Brian Phillips bpphillips+ml at gmail.com
Sat Nov 15 16:36:06 GMT 2008


Hello all,
I've checked in a first draft of a DBIx::Class component providing
optimistic locking support and I would appreciate any feedback you might
have.  Check it out here:
http://dev.catalyst.perl.org/repos/bast/DBIx-Class-OptimisticLocking/1.000/=
trunk

>From the README:

DBIx::Class::OptimisticLocking - Optimistic locking support for DBIx::Class

Optimistic locking is an alternative to using exclusive locks when you have
the possibility of concurrent, conflicting updates in your database.  The
basic principle is you allow any and all
clients to issue updates and rather than preemptively synchronizing all data
modifications (which is what happens with exclusive locks) you are
"optimistic" that updates won't interfere with one
another and the updates will only fail when they do in fact interfere with
one another.

Consider the following scenario (in timeline order, not in the same block of
code):

       my $order =3D $schema->resultset('Orders')->find(1);

       # some other different, concurrent process loads the same object
       my $other_order =3D $schema->resultset('Orders')->find(1);

       $order->status('fraud review');
       $other_order->status('processed');

       $order->update; # this succeeds
       $other_order->update; # this fails when using optimistic locking

Without optimistic locking (or exclusive locking), the example order would
have two sequential updates issued with the second essentially erasing the
results of the first.  With optimistic
locking, the second update (on $other_order) would fail.

This optimistic locking is typically done by adding additional restrictions
to the "WHERE" clause of the "UPDATE" statement.  These additional
restrictions ensure the data is still in the
expected state before applying the update.  This
DBIx::Class::OptimisticLocking component provides a few different strategies
for providing this functionality.

Thanks!

Brian Phillips
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20081115/250=
61fb7/attachment.htm


More information about the DBIx-Class mailing list