|
| 1 | +package com.baeldung.hibernate.optimisticlocking; |
| 2 | + |
| 3 | +import com.baeldung.hibernate.HibernateUtil; |
| 4 | +import org.junit.After; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import javax.persistence.EntityManager; |
| 9 | +import javax.persistence.LockModeType; |
| 10 | +import javax.persistence.OptimisticLockException; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Arrays; |
| 13 | + |
| 14 | +public class OptimisticLockingIntegrationTest { |
| 15 | + |
| 16 | + @Before |
| 17 | + public void setUp() throws IOException { |
| 18 | + EntityManager entityManager = getEntityManagerWithOpenTransaction(); |
| 19 | + OptimisticLockingCourse course = new OptimisticLockingCourse(1L, "MATH"); |
| 20 | + OptimisticLockingStudent student = new OptimisticLockingStudent(1L, "John", "Doe", Arrays.asList(course)); |
| 21 | + course.setStudent(student); |
| 22 | + entityManager.persist(course); |
| 23 | + entityManager.persist(student); |
| 24 | + entityManager.getTransaction() |
| 25 | + .commit(); |
| 26 | + entityManager.close(); |
| 27 | + } |
| 28 | + |
| 29 | + @After |
| 30 | + public void clean() throws IOException { |
| 31 | + EntityManager entityManager = getEntityManagerWithOpenTransaction(); |
| 32 | + OptimisticLockingCourse course = entityManager.find(OptimisticLockingCourse.class, 1L); |
| 33 | + OptimisticLockingStudent student = entityManager.find(OptimisticLockingStudent.class, 1L); |
| 34 | + entityManager.remove(course); |
| 35 | + entityManager.remove(student); |
| 36 | + entityManager.getTransaction() |
| 37 | + .commit(); |
| 38 | + entityManager.close(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test(expected = OptimisticLockException.class) |
| 42 | + public void givenVersionedEntities_whenConcurrentUpdate_thenOptimisticLockException() throws IOException { |
| 43 | + EntityManager em = getEntityManagerWithOpenTransaction(); |
| 44 | + OptimisticLockingStudent student = em.find(OptimisticLockingStudent.class, 1L); |
| 45 | + |
| 46 | + EntityManager em2 = getEntityManagerWithOpenTransaction(); |
| 47 | + OptimisticLockingStudent student2 = em2.find(OptimisticLockingStudent.class, 1L); |
| 48 | + student2.setName("RICHARD"); |
| 49 | + em2.persist(student2); |
| 50 | + em2.getTransaction() |
| 51 | + .commit(); |
| 52 | + em2.close(); |
| 53 | + |
| 54 | + student.setName("JOHN"); |
| 55 | + em.persist(student); |
| 56 | + em.getTransaction() |
| 57 | + .commit(); |
| 58 | + em.close(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test(expected = OptimisticLockException.class) |
| 62 | + public void givenVersionedEntitiesWithLockByFindMethod_whenConcurrentUpdate_thenOptimisticLockException() throws IOException { |
| 63 | + EntityManager em = getEntityManagerWithOpenTransaction(); |
| 64 | + OptimisticLockingStudent student = em.find(OptimisticLockingStudent.class, 1L, LockModeType.OPTIMISTIC); |
| 65 | + |
| 66 | + EntityManager em2 = getEntityManagerWithOpenTransaction(); |
| 67 | + OptimisticLockingStudent student2 = em2.find(OptimisticLockingStudent.class, 1L, LockModeType.OPTIMISTIC_FORCE_INCREMENT); |
| 68 | + student2.setName("RICHARD"); |
| 69 | + em2.persist(student2); |
| 70 | + em2.getTransaction() |
| 71 | + .commit(); |
| 72 | + em2.close(); |
| 73 | + |
| 74 | + student.setName("JOHN"); |
| 75 | + em.persist(student); |
| 76 | + em.getTransaction() |
| 77 | + .commit(); |
| 78 | + em.close(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test(expected = OptimisticLockException.class) |
| 82 | + public void givenVersionedEntitiesWithLockByRefreshMethod_whenConcurrentUpdate_thenOptimisticLockException() throws IOException { |
| 83 | + EntityManager em = getEntityManagerWithOpenTransaction(); |
| 84 | + OptimisticLockingStudent student = em.find(OptimisticLockingStudent.class, 1L); |
| 85 | + em.refresh(student, LockModeType.OPTIMISTIC); |
| 86 | + |
| 87 | + EntityManager em2 = getEntityManagerWithOpenTransaction(); |
| 88 | + OptimisticLockingStudent student2 = em2.find(OptimisticLockingStudent.class, 1L); |
| 89 | + em.refresh(student, LockModeType.OPTIMISTIC_FORCE_INCREMENT); |
| 90 | + student2.setName("RICHARD"); |
| 91 | + em2.persist(student2); |
| 92 | + em2.getTransaction() |
| 93 | + .commit(); |
| 94 | + em2.close(); |
| 95 | + |
| 96 | + student.setName("JOHN"); |
| 97 | + em.persist(student); |
| 98 | + em.getTransaction() |
| 99 | + .commit(); |
| 100 | + em.close(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test(expected = OptimisticLockException.class) |
| 104 | + public void givenVersionedEntitiesWithLockByLockMethod_whenConcurrentUpdate_thenOptimisticLockException() throws IOException { |
| 105 | + EntityManager em = getEntityManagerWithOpenTransaction(); |
| 106 | + OptimisticLockingStudent student = em.find(OptimisticLockingStudent.class, 1L); |
| 107 | + em.lock(student, LockModeType.OPTIMISTIC); |
| 108 | + |
| 109 | + EntityManager em2 = getEntityManagerWithOpenTransaction(); |
| 110 | + OptimisticLockingStudent student2 = em2.find(OptimisticLockingStudent.class, 1L); |
| 111 | + em.lock(student, LockModeType.OPTIMISTIC_FORCE_INCREMENT); |
| 112 | + student2.setName("RICHARD"); |
| 113 | + em2.persist(student2); |
| 114 | + em2.getTransaction() |
| 115 | + .commit(); |
| 116 | + em2.close(); |
| 117 | + |
| 118 | + student.setName("JOHN"); |
| 119 | + em.persist(student); |
| 120 | + em.getTransaction() |
| 121 | + .commit(); |
| 122 | + em.close(); |
| 123 | + } |
| 124 | + |
| 125 | + protected static EntityManager getEntityManagerWithOpenTransaction() throws IOException { |
| 126 | + String propertyFileName = "hibernate-pessimistic-locking.properties"; |
| 127 | + EntityManager entityManager = HibernateUtil.getSessionFactory(propertyFileName) |
| 128 | + .openSession(); |
| 129 | + entityManager.getTransaction() |
| 130 | + .begin(); |
| 131 | + |
| 132 | + return entityManager; |
| 133 | + } |
| 134 | +} |
0 commit comments