How JPA knows when it have to throw OptimisticLock

2019-09-04 09:07发布

I would like to have confirmation of the following :

public void test() {

   SomeEntity se = em.find(SomeEntity.class, 1);

   // Do something...

   se.setField = "test";

}

Here JPA will perform only optimistic check on se.

Next :

public void test() {

   SomeEntity se = em.find(SomeEntity.class, 1);
   SomeOtherEntity soe = em.find(SomeOtherEntity.class, 1);
   em.lock(soe, LockModeType.OPTIMISTIC);

   // Do something...

   se.setField = "test";

}

Here JPA will throw OptimisticException if se OR soe have been changed (version incremented in database) since last read.

Is that true ?

标签: java jpa
1条回答
Luminary・发光体
2楼-- · 2019-09-04 09:42

OptimisticLockException will be throw only when transaction commits to database ore you call em.flush() - then JPA Provider will compare version field and figure out that it was changed by another transactoin.

查看更多
登录 后发表回答