BAEL-1713: Pessimistic Locking in JPA
Improvments for tests.
This commit is contained in:
parent
c0c319b13f
commit
fd11e089fe
|
@ -17,52 +17,62 @@ public class PessimisticLockScopesIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException {
|
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException {
|
||||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||||
PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "A", "B", new BigDecimal(4.5));
|
PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "JOHN", "SMITH", new BigDecimal(4.5));
|
||||||
em.persist(employee);
|
em.persist(employee);
|
||||||
em.getTransaction()
|
em.getTransaction()
|
||||||
.commit();
|
.commit();
|
||||||
em.close();
|
em.close();
|
||||||
|
|
||||||
// NORMAL SCOPE
|
// NORMAL SCOPE
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||||
PessimisticLockingEmployee foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
PessimisticLockingEmployee foundEmployee = em2.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||||
em.getTransaction()
|
em2.getTransaction()
|
||||||
.rollback();
|
.rollback();
|
||||||
|
|
||||||
// EXTENDED SCOPE
|
// EXTENDED SCOPE
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||||
|
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||||
foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
foundEmployee = em3.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||||
|
em3.getTransaction()
|
||||||
|
.rollback();
|
||||||
|
|
||||||
|
em2.close();
|
||||||
|
em3.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException {
|
public void givenEntityWithElementCollection_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
|
||||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||||
Address address = new Address("Poland", "Warsaw");
|
Address address = new Address("Poland", "Warsaw");
|
||||||
Customer customer = new Customer(1L, "A", "B", Arrays.asList(address));
|
Customer customer = new Customer(1L, "JOE", "DOE", Arrays.asList(address));
|
||||||
em.persist(customer);
|
em.persist(customer);
|
||||||
em.getTransaction()
|
em.getTransaction()
|
||||||
.commit();
|
.commit();
|
||||||
em.close();
|
em.close();
|
||||||
|
|
||||||
// NORMAL SCOPE
|
// NORMAL SCOPE
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||||
Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||||
em.getTransaction()
|
em2.getTransaction()
|
||||||
.rollback();
|
.rollback();
|
||||||
|
|
||||||
// EXTENDED SCOPE
|
// EXTENDED SCOPE
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||||
|
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||||
foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||||
|
em2.getTransaction()
|
||||||
|
.rollback();
|
||||||
|
|
||||||
|
em2.close();
|
||||||
|
em3.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException {
|
public void givenEntityWithOneToMany_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
|
||||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||||
PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE");
|
PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE");
|
||||||
PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student);
|
PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student);
|
||||||
|
@ -74,17 +84,22 @@ public class PessimisticLockScopesIntegrationTest {
|
||||||
em.close();
|
em.close();
|
||||||
|
|
||||||
// NORMAL SCOPE
|
// NORMAL SCOPE
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||||
PessimisticLockingCourse foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
PessimisticLockingCourse foundCourse = em2.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||||
em.getTransaction()
|
em2.getTransaction()
|
||||||
.rollback();
|
.rollback();
|
||||||
|
|
||||||
// EXTENDED SCOPE
|
// EXTENDED SCOPE
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||||
|
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||||
foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
foundCourse = em3.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||||
|
em3.getTransaction()
|
||||||
|
.rollback();
|
||||||
|
|
||||||
|
em2.close();
|
||||||
|
em3.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected EntityManager getEntityManagerWithOpenTransaction() throws IOException {
|
protected EntityManager getEntityManagerWithOpenTransaction() throws IOException {
|
||||||
|
|
|
@ -22,54 +22,64 @@ public class PessimisticLockScopesIntegrationTest {
|
||||||
EntityManagerFactory entityManagerFactory;
|
EntityManagerFactory entityManagerFactory;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() {
|
public void givenEntityWithJoinInheritance_whenLock_thenNormalAndExtendScopesLockParentAndChildEntity() {
|
||||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||||
Employee employee = new Employee(1L, "A", "B", new BigDecimal(4.5));
|
Employee employee = new Employee(1L, "JOE", "DOE", new BigDecimal(4.5));
|
||||||
em.persist(employee);
|
em.persist(employee);
|
||||||
em.getTransaction()
|
em.getTransaction()
|
||||||
.commit();
|
.commit();
|
||||||
em.close();
|
em.close();
|
||||||
|
|
||||||
// NORMAL SCOPE
|
// NORMAL SCOPE
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||||
Employee foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
Employee foundEmployee = em2.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||||
em.getTransaction()
|
em2.getTransaction()
|
||||||
.rollback();
|
.rollback();
|
||||||
|
|
||||||
// EXTENDED SCOPE
|
// EXTENDED SCOPE
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||||
|
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||||
foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
foundEmployee = em3.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||||
|
em3.getTransaction()
|
||||||
|
.rollback();
|
||||||
|
|
||||||
|
em2.close();
|
||||||
|
em3.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() {
|
public void givenEntityWithElementCollection_whenLock_thenExtendScopeLocksAlsoCollectionTable() {
|
||||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||||
Address address = new Address("Poland", "Warsaw");
|
Address address = new Address("Poland", "Warsaw");
|
||||||
Customer customer = new Customer(1L, "A", "B", Arrays.asList(address));
|
Customer customer = new Customer(1L, "JOHN", "SMITH", Arrays.asList(address));
|
||||||
em.persist(customer);
|
em.persist(customer);
|
||||||
em.getTransaction()
|
em.getTransaction()
|
||||||
.commit();
|
.commit();
|
||||||
em.close();
|
em.close();
|
||||||
|
|
||||||
// NORMAL SCOPE
|
// NORMAL SCOPE
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||||
Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||||
em.getTransaction()
|
em2.getTransaction()
|
||||||
.rollback();
|
.rollback();
|
||||||
|
|
||||||
// EXTENDED SCOPE
|
// EXTENDED SCOPE
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||||
|
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||||
foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||||
|
em3.getTransaction()
|
||||||
|
.rollback();
|
||||||
|
|
||||||
|
em2.close();
|
||||||
|
em3.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() {
|
public void givenEclipseEntityWithOneToMany_whenLock_thenExtendedLockAlsoJoinTable() {
|
||||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||||
Student student = new Student(1L, "JOE");
|
Student student = new Student(1L, "JOE");
|
||||||
Course course = new Course(1L, "COURSE", student);
|
Course course = new Course(1L, "COURSE", student);
|
||||||
|
@ -81,17 +91,22 @@ public class PessimisticLockScopesIntegrationTest {
|
||||||
em.close();
|
em.close();
|
||||||
|
|
||||||
// NORMAL SCOPE
|
// NORMAL SCOPE
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||||
Course foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
Course foundCourse = em2.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||||
em.getTransaction()
|
em2.getTransaction()
|
||||||
.rollback();
|
.rollback();
|
||||||
|
|
||||||
// EXTENDED SCOPE
|
// EXTENDED SCOPE
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||||
|
|
||||||
em = getEntityManagerWithOpenTransaction();
|
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||||
foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
foundCourse = em3.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||||
|
em3.getTransaction()
|
||||||
|
.rollback();
|
||||||
|
|
||||||
|
em2.close();
|
||||||
|
em3.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected EntityManager getEntityManagerWithOpenTransaction() {
|
protected EntityManager getEntityManagerWithOpenTransaction() {
|
||||||
|
|
Loading…
Reference in New Issue