0

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 ?

Olivier J.
  • 3,115
  • 11
  • 48
  • 71
  • Not sure about when the OLE is thrown. I believe it's thrown after `se` or `soe` have been changed **and** you try to update them. I mean `se.setField("New Value");` alone won't throw the exception but `se.setField("New Value"); em.merge(se); em.flush();` will. – Jonathan Morales Vélez Feb 17 '14 at 20:42

1 Answers1

0

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.

Javasick
  • 2,753
  • 1
  • 23
  • 35