Skip to content

Commit fd11e08

Browse files
BAEL-1713: Pessimistic Locking in JPA
Improvments for tests.
1 parent c0c319b commit fd11e08

2 files changed

Lines changed: 69 additions & 39 deletions

File tree

‎hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java‎

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,62 @@ public class PessimisticLockScopesIntegrationTest {
1717
@Test
1818
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException {
1919
EntityManager em = getEntityManagerWithOpenTransaction();
20-
PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "A", "B", new BigDecimal(4.5));
20+
PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "JOHN", "SMITH", new BigDecimal(4.5));
2121
em.persist(employee);
2222
em.getTransaction()
2323
.commit();
2424
em.close();
2525

2626
// NORMAL SCOPE
27-
em = getEntityManagerWithOpenTransaction();
28-
PessimisticLockingEmployee foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
29-
em.getTransaction()
27+
EntityManager em2 = getEntityManagerWithOpenTransaction();
28+
PessimisticLockingEmployee foundEmployee = em2.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
29+
em2.getTransaction()
3030
.rollback();
3131

3232
// EXTENDED SCOPE
3333
Map<String, Object> map = new HashMap<>();
3434
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
3535

36-
em = getEntityManagerWithOpenTransaction();
37-
foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
36+
EntityManager em3 = getEntityManagerWithOpenTransaction();
37+
foundEmployee = em3.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
38+
em3.getTransaction()
39+
.rollback();
40+
41+
em2.close();
42+
em3.close();
3843
}
3944

4045
@Test
41-
public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException {
46+
public void givenEntityWithElementCollection_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
4247
EntityManager em = getEntityManagerWithOpenTransaction();
4348
Address address = new Address("Poland", "Warsaw");
44-
Customer customer = new Customer(1L, "A", "B", Arrays.asList(address));
49+
Customer customer = new Customer(1L, "JOE", "DOE", Arrays.asList(address));
4550
em.persist(customer);
4651
em.getTransaction()
4752
.commit();
4853
em.close();
4954

5055
// NORMAL SCOPE
51-
em = getEntityManagerWithOpenTransaction();
52-
Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
53-
em.getTransaction()
56+
EntityManager em2 = getEntityManagerWithOpenTransaction();
57+
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
58+
em2.getTransaction()
5459
.rollback();
5560

5661
// EXTENDED SCOPE
5762
Map<String, Object> map = new HashMap<>();
5863
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
5964

60-
em = getEntityManagerWithOpenTransaction();
61-
foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
65+
EntityManager em3 = getEntityManagerWithOpenTransaction();
66+
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
67+
em2.getTransaction()
68+
.rollback();
69+
70+
em2.close();
71+
em3.close();
6272
}
6373

6474
@Test
65-
public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException {
75+
public void givenEntityWithOneToMany_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
6676
EntityManager em = getEntityManagerWithOpenTransaction();
6777
PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE");
6878
PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student);
@@ -74,17 +84,22 @@ public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwn
7484
em.close();
7585

7686
// NORMAL SCOPE
77-
em = getEntityManagerWithOpenTransaction();
78-
PessimisticLockingCourse foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
79-
em.getTransaction()
87+
EntityManager em2 = getEntityManagerWithOpenTransaction();
88+
PessimisticLockingCourse foundCourse = em2.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
89+
em2.getTransaction()
8090
.rollback();
8191

8292
// EXTENDED SCOPE
8393
Map<String, Object> map = new HashMap<>();
8494
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
8595

86-
em = getEntityManagerWithOpenTransaction();
87-
foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
96+
EntityManager em3 = getEntityManagerWithOpenTransaction();
97+
foundCourse = em3.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
98+
em3.getTransaction()
99+
.rollback();
100+
101+
em2.close();
102+
em3.close();
88103
}
89104

90105
protected EntityManager getEntityManagerWithOpenTransaction() throws IOException {

‎persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,64 @@ public class PessimisticLockScopesIntegrationTest {
2222
EntityManagerFactory entityManagerFactory;
2323

2424
@Test
25-
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() {
25+
public void givenEntityWithJoinInheritance_whenLock_thenNormalAndExtendScopesLockParentAndChildEntity() {
2626
EntityManager em = getEntityManagerWithOpenTransaction();
27-
Employee employee = new Employee(1L, "A", "B", new BigDecimal(4.5));
27+
Employee employee = new Employee(1L, "JOE", "DOE", new BigDecimal(4.5));
2828
em.persist(employee);
2929
em.getTransaction()
3030
.commit();
3131
em.close();
3232

3333
// NORMAL SCOPE
34-
em = getEntityManagerWithOpenTransaction();
35-
Employee foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
36-
em.getTransaction()
34+
EntityManager em2 = getEntityManagerWithOpenTransaction();
35+
Employee foundEmployee = em2.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
36+
em2.getTransaction()
3737
.rollback();
3838

3939
// EXTENDED SCOPE
4040
Map<String, Object> map = new HashMap<>();
4141
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
4242

43-
em = getEntityManagerWithOpenTransaction();
44-
foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
43+
EntityManager em3 = getEntityManagerWithOpenTransaction();
44+
foundEmployee = em3.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
45+
em3.getTransaction()
46+
.rollback();
47+
48+
em2.close();
49+
em3.close();
4550
}
4651

4752
@Test
48-
public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() {
53+
public void givenEntityWithElementCollection_whenLock_thenExtendScopeLocksAlsoCollectionTable() {
4954
EntityManager em = getEntityManagerWithOpenTransaction();
5055
Address address = new Address("Poland", "Warsaw");
51-
Customer customer = new Customer(1L, "A", "B", Arrays.asList(address));
56+
Customer customer = new Customer(1L, "JOHN", "SMITH", Arrays.asList(address));
5257
em.persist(customer);
5358
em.getTransaction()
5459
.commit();
5560
em.close();
5661

5762
// NORMAL SCOPE
58-
em = getEntityManagerWithOpenTransaction();
59-
Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
60-
em.getTransaction()
63+
EntityManager em2 = getEntityManagerWithOpenTransaction();
64+
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
65+
em2.getTransaction()
6166
.rollback();
6267

6368
// EXTENDED SCOPE
6469
Map<String, Object> map = new HashMap<>();
6570
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
6671

67-
em = getEntityManagerWithOpenTransaction();
68-
foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
72+
EntityManager em3 = getEntityManagerWithOpenTransaction();
73+
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
74+
em3.getTransaction()
75+
.rollback();
76+
77+
em2.close();
78+
em3.close();
6979
}
7080

7181
@Test
72-
public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() {
82+
public void givenEclipseEntityWithOneToMany_whenLock_thenExtendedLockAlsoJoinTable() {
7383
EntityManager em = getEntityManagerWithOpenTransaction();
7484
Student student = new Student(1L, "JOE");
7585
Course course = new Course(1L, "COURSE", student);
@@ -81,17 +91,22 @@ public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwn
8191
em.close();
8292

8393
// NORMAL SCOPE
84-
em = getEntityManagerWithOpenTransaction();
85-
Course foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE);
86-
em.getTransaction()
94+
EntityManager em2 = getEntityManagerWithOpenTransaction();
95+
Course foundCourse = em2.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE);
96+
em2.getTransaction()
8797
.rollback();
8898

8999
// EXTENDED SCOPE
90100
Map<String, Object> map = new HashMap<>();
91101
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
92102

93-
em = getEntityManagerWithOpenTransaction();
94-
foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
103+
EntityManager em3 = getEntityManagerWithOpenTransaction();
104+
foundCourse = em3.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
105+
em3.getTransaction()
106+
.rollback();
107+
108+
em2.close();
109+
em3.close();
95110
}
96111

97112
protected EntityManager getEntityManagerWithOpenTransaction() {

0 commit comments

Comments
 (0)