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 <w...@gmail.com> 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 赵俊 <zh...@jd.com> wrote:> > >> > > It seems that adding a version field in product entity will work.> > >> > > > On 10 Sep 2018, at 11:43 AM, Willem Jiang <wi...@gmail.com> 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