[CDBI] Re: construct sucks.

Perrin Harkins perrin at elem.com
Mon Aug 28 20:36:32 BST 2006


On Mon, 2006-08-28 at 19:56 +0100, Matt S Trout wrote:
> But how can you know it's obsolete?

Because if you don't want overwrites to happen, you have to use locks,
and if you aren't using locks, you will have this problem everywhere and
you presumably don't care.  This specific condition only happens if you
load some data that you intend to update without locking it, change it
in memory without acquiring a lock, and then load it again before saving
it.

You have 3 choices when loading data from a db and modifying it in a
program:
1) Use pessimistic locking, where you acquire an actual database lock on
records you intend to change ("SELECT ... FOR UPDATE").
2) Use optimistic locking and implement some kind version number check
at save time to tell if someone else changed data under you (or use an
"UPDATE... WHERE values=old_values" approach).
3) Use no locking at all and let the last save win.

You could get into this situation with newer data coming from the db if
you chose #3, but you wouldn't care.  You could get into it with #2, but
the version check at save time would catch it and prevent the save.  You
couldn't have it happen with #1.

Most people choose #3 without properly thinking about it, but that's no
reason to throw an exception.

- Perrin




More information about the ClassDBI mailing list