Return-Path: X-Original-To: apmail-cayenne-user-archive@www.apache.org Delivered-To: apmail-cayenne-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D0E359982 for ; Tue, 21 Feb 2012 16:51:53 +0000 (UTC) Received: (qmail 42350 invoked by uid 500); 21 Feb 2012 16:51:53 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 42330 invoked by uid 500); 21 Feb 2012 16:51:53 -0000 Mailing-List: contact user-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cayenne.apache.org Delivered-To: mailing list user@cayenne.apache.org Received: (qmail 42322 invoked by uid 99); 21 Feb 2012 16:51:53 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Feb 2012 16:51:53 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [89.245.139.70] (HELO mail01.hennig-fahrzeugteile.de) (89.245.139.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Feb 2012 16:51:46 +0000 X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: RE: Optimistic locking Date: Tue, 21 Feb 2012 17:51:25 +0100 Message-ID: In-Reply-To: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Optimistic locking Thread-Index: Aczwtf6KKLNOpeZqTeOwBZUaRJ/CiwAABozw References: <0A1740BF-44E2-4506-B8B4-05CD4E68BD40@objectstyle.org> <43DDB664-64BC-4A61-B219-B67DEC9AF09C@objectstyle.org> <20E46D04-051E-429D-81C3-473C34D7F686@objectstyle.org> From: "Durchholz, Joachim" To: X-Virus-Checked: Checked by ClamAV on apache.org > The short answer is it really depends on your = application/requirements. > For my personal preference, I like to lock on all meaningful columns > with the exception of blobs unless there is a compelling data/business > requirement to not do so. You also need to be careful of triggers = that > update things behind the scenes, too. If you have a trigger that = updates > a last-modified column, you wouldn't want to optimistically lock on = that > column because the trigger is going to be changing the value behind = your > back. So my understanding is that Cayenne leaves it to the application to implement optimistic locking. I'm not sure how to do that well with Cayenne. My approach would be: 1) lock all relevant rows 2) load the relevant rows as Cayenne entities 3) compare for differences with the unmodified state 4) if all goes well, trigger Cayenne's commit. (1) means I need to know all rows that might be affected. Cayenne tells = me about entities; does it tell me about the rows associated with them? In the presence of flattened relationships, too? (2) means opening another Cayenne context and loading the data I guess. That should work out of the box. (3) means a deep traversal of object networks, while avoiding triggering = a lazy load. Not sure which parts of the Cayenne API to inspect for that. Also, not sure how to get that done without writing lots of boilerplate code. (4) should work out of the box. So steps (1) and (3) are the ones I don't know how to do. ------------- There's an approach that halves the number of server roundtrips: Use UPDATE table SET field =3D :value WHERE pk =3D :id AND field =3D :old_value and check whether that returns an update count of 1 - if field got = changed by another process in the time between local read and update, the UPDATE will find no record that matches the criteria, do nothing, and return a = 0. Is there a way to do that in Cayenne? (This technique is the one that the Hibernate folks use for their implementation of optimistic locking.)