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 5DCAF19F1F for ; Fri, 29 Apr 2016 20:25:35 +0000 (UTC) Received: (qmail 10619 invoked by uid 500); 29 Apr 2016 20:25:35 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 10593 invoked by uid 500); 29 Apr 2016 20:25:34 -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 10581 invoked by uid 99); 29 Apr 2016 20:25:34 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Apr 2016 20:25:34 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 08A981A4726 for ; Fri, 29 Apr 2016 20:25:34 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.197 X-Spam-Level: * X-Spam-Status: No, score=1.197 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HTML_MESSAGE=2, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd2-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=yahoo.com Received: from mx2-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id vopI6ox9Si8Z for ; Fri, 29 Apr 2016 20:25:30 +0000 (UTC) Received: from nm43-vm5.bullet.mail.bf1.yahoo.com (nm43-vm5.bullet.mail.bf1.yahoo.com [216.109.114.236]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with ESMTPS id 224185F296 for ; Fri, 29 Apr 2016 20:25:30 +0000 (UTC) Received: from [66.196.81.170] by nm43.bullet.mail.bf1.yahoo.com with NNFMP; 29 Apr 2016 20:25:23 -0000 Received: from [98.139.212.246] by tm16.bullet.mail.bf1.yahoo.com with NNFMP; 29 Apr 2016 20:25:23 -0000 Received: from [127.0.0.1] by omp1055.mail.bf1.yahoo.com with NNFMP; 29 Apr 2016 20:25:23 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 378392.47867.bm@omp1055.mail.bf1.yahoo.com X-YMail-OSG: UaS6USgVM1lSthU_mn2Fmby6uu7.beniqlmy_HHJ0raSjlAq9QgWn7SplHsztOQ BR1SeH7Eh8LoWUSTWm0bonPaSH0tLer9qmM4Nut0c0q5UD4jIX7qFTo0QLTJ_zzasBdM.26vy39g EvpE3sDhPKyFL50iZCSIorF.kiVIYAjs.h1nOteG5GlPPgdhz5YuUddPKpfFx.4ciZPNyxJi.dMB L5f4uL86WodLnrWcL.xoRdZ5Xo.w8qgFb3YL5JB.pMGfPSrlCr5KK_GLy9HBK6j1hi9ZSJ_7K3Mc .pY4VR.U.i6Eje0MBA3xoJTVV2UC2GfaYr369whVh0.X35JSlzkvuzZJwcaqVvOAoDxQ2qSMndbN S23aUfe9gVFITFUOvQ20x0C6lJYudO3HMFCzBKfm7C0F8nCgxmzhUMVc3HNfPb.W4JhMSY9wvgrS j7yc3_BNy_MfmKWvNckgvmdmam6QohVgAY3x55siNp0n6.Zi94y8BRYvfgkpOpWAvfkNrYTqoCRy TyuLqtg6A5KzSs2BnyzUqcCNnQTWjEog- Received: by 76.13.26.143; Fri, 29 Apr 2016 20:25:22 +0000 Date: Fri, 29 Apr 2016 20:25:20 +0000 (UTC) From: Juan Manuel Diaz Lara Reply-To: Juan Manuel Diaz Lara To: "user@cayenne.apache.org" Message-ID: <142279776.5117643.1461961520933.JavaMail.yahoo@mail.yahoo.com> In-Reply-To: References: Subject: Re: How does optimistic locking works ? MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_5117642_1117963259.1461961520929" ------=_Part_5117642_1117963259.1461961520929 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thanks for your reply.... to clarify, my version field is marked as the onl= y field used for optimistic locking, and the entity was market too for opti= mistic locking, the problem was that I was expecting cayenne to manage the = version field, but the only thing it does is to add the condition to the wh= ere clause to implement the verification part of the optimistic locking str= ategy. As I wrote in a previous mail, the solution is to increment the version fie= ld ourself (from the app code) just before we commit, and for this there co= uld be several strategies. =C2=A0Atte. Juan Manuel D=C3=ADaz Lara=20 On Friday, April 29, 2016 8:48 AM, Michael Gentry = wrote: =20 Hi Juan, Typically in Cayenne you wouldn't have a version field.=C2=A0 You'd instead optimistically lock on "units" and other values. Assuming you optimistically locked on "units" and using the example code you gave, you would only get an exception if some other activity changed the value of "units" in the database: a) after your initial fetch and before your first commitChanges b) after your first commitChanges and before your second commitChanges If you aren't familiar with optimistic locking, in Cayenne Modeler make sure you have "Optimistic Locking" checked on your StockCurrent ObjEntity (under the "Entity" tab), then also make sure you have "Used for Locking" checked (under the "Attributes" tab) for "user" and other attributes. Cayenne will then verify those checked attributes have not changed when you do a commitChanges.=C2=A0 You don't need the version field unless you have = a requirement to know how many times something has been updated, but that's not a very good requirement since you probably really want to know what has changed instead, which is an entirely different discussion. mrg On Thu, Apr 28, 2016 at 10:31 PM, Juan Manuel Diaz Lara < jmdiazlr@yahoo.com.invalid> wrote: > Ok, I was thinking on other frameworks... the simple solution is to > increment the version field from the app before commitChanges() (anyway t= he > commit will only succeed if the orinal value of the version field is equa= l > in the db). I made this on a preUpdate lifecycle event to not forget to d= o > it and ensure a monotonically increment. > To anyone maybe interested. >=C2=A0 Atte. Juan Manuel D=C3=ADaz Lara > >=C2=A0 =C2=A0 On Thursday, April 28, 2016 5:52 PM, Juan Manuel Diaz Lara > wrote: > > >=C2=A0 I have this simplified scenario where the second commitChanges fail= s with > an optimistic exception: > >=C2=A0 =C2=A0 =C2=A0 =C2=A0 s =3D CayenneDao.instance.getByPrimaryKey(Stoc= kCurrent.class, > testStockcurrentId); >=C2=A0 =C2=A0 =C2=A0 =C2=A0 s.setUnits(s.getUnits()+1)); >=C2=A0 =C2=A0 =C2=A0 =C2=A0 context.commitChanges(); >=C2=A0 =C2=A0 =C2=A0 =C2=A0 s.setUnits(s.getUnits()+ 7); >=C2=A0 =C2=A0 =C2=A0 =C2=A0 context.commitChanges(); >=C2=A0 I have traced the log and I can see that the version field con s is= not > updated after first commit, that is annoying because it break's the secon= d > commit. > > So the question is: what is the cayenne contract in relation to optimisti= c > locking ? what does it do ? only testing when committing ? does not updat= e > version field nor refresh it from db when commiting ? > In my db, I have a version field in every table (integer), and a trigger > that updates it if not done from the app, any way, how can I make this to > work ? > > > Atte. Juan Manuel D=C3=ADaz Lara > > > ------=_Part_5117642_1117963259.1461961520929--