It sounds good, with the help of ObjectOptimisticLockingFailureException, we could do the compare and set (CAS) as the no-blocking algorithm does to the retry.
Willem Jiang Twitter: willemjiang Weibo: 姜宁willem On Fri, Sep 14, 2018 at 10:54 AM 黎 先生 <[email protected]> wrote: > > Adding a opmistic lock to the entity class like this: > > @Version > private Integer version; > > When productDao.save(product) is invoked, hibernate (or any other > implementations) should generate sql like this : > update ... set ... where id = ? and version = ?; > > If there is nothing to update, which means the object has been updated by > another thread, ObjectOptimisticLockingFailureException is thrown. > ________________________________ > 发件人: Sean Yin <[email protected]> > 发送时间: 2018年9月13日 18:11 > 收件人: [email protected] > 主题: Re: ServiceComb Saga TCC demo with JPA > > I think that's normally done with @Version annotation on a version field. > And JPA will take care of concurrent update. > > On 2018/09/10 07:44:05, Willem Jiang <[email protected]> wrote: > > Yes, it should work, but I need to know how to write the code :)> > > > > Willem Jiang> > > > > Twitter: willemjiang> > > Weibo: 姜宁willem> > > > > On Mon, Sep 10, 2018 at 2:01 PM 赵俊 <[email protected]> wrote:> > > >> > > > It seems that adding a version field in product entity will work.> > > >> > > > > On 10 Sep 2018, at 11:43 AM, Willem Jiang <[email protected]> wrote:> > > > >> > > > > Hi,> > > > >> > > > > I submitted a PR[1] to show how to use TCC with JPA. There are some> > > > > missing points with my limited knowledge of JPA. So I just send out> > > > > this email to ask help from community.> > > > >> > > > > In the inventory service[2], we need to update the product in stock> > > > > number[3]. As the reserver method could be invoked in multiple> > > > > threads at the same time and JPA doesn't provide the isolation across> > > > > > these thread[4]. We may need to add optimis lock or use version to> > > > > keep track for the change. But I didn't manage to get workable code> > > > > by applying the optimic lock or using the version file to prevent the> > > > > > lost updates[5].> > > > >> > > > > So I'm asking help from the expert of JPA for an example of this > case.> > > > >> > > > > [1]https://github.com/apache/incubator-servicecomb-saga/pull/290> > > > > [2] > https://github.com/apache/incubator-servicecomb-saga/tree/SCB-820/saga-demo/tcc-spring-demo/inventory > > > > > > [3] > https://github.com/apache/incubator-servicecomb-saga/blob/SCB-820/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/InventoryService.java#L44 > > > > > > [4] > https://vladmihalcea.com/a-beginners-guide-to-database-locking-and-the-lost-update-phenomena/ > > > > > > [5] > https://vladmihalcea.com/preventing-lost-updates-in-long-conversations/> > > > >> > > > >> > > > > Willem Jiang> > > > >> > > > > Twitter: willemjiang> > > > > Weibo: 姜宁willem> > > >> > > > > Best Regards, > Sean
