Created
June 22, 2016 16:01
-
-
Save tkfx/1953955c59213872ebdb809cdbcae950 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // EntityManager and transaction 1 | |
| EntityManager em = emf.createEntityManager(); | |
| em.getTransaction().begin(); | |
| // EntityManager and transaction 2 | |
| EntityManager em2 = emf.createEntityManager(); | |
| em2.getTransaction().begin(); | |
| // update 1 | |
| Author a = em.find(Author.class, 1L); | |
| a.setFirstName("changed"); | |
| // update 2 | |
| Author a2 = em2.find(Author.class, 1L); | |
| a2.setFirstName("changed"); | |
| // commit transaction 1 | |
| em.getTransaction().commit(); | |
| em.close(); | |
| // commit transaction 2 | |
| try { | |
| em2.getTransaction().commit(); | |
| Assert.fail(); | |
| } catch (RollbackException e) { | |
| Assert.assertTrue(e.getCause() instanceof OptimisticLockException); | |
| log.info("2nd transaction failed with an OptimisticLockException"); | |
| } | |
| em2.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment