BAEL-1713: Pessimistic Locking in JPA

Improvments for tests.
This commit is contained in:
Patryk Kucharz 2018-05-10 20:53:58 +02:00
parent c0c319b13f
commit fd11e089fe
2 changed files with 69 additions and 39 deletions

View File

@ -17,52 +17,62 @@ public class PessimisticLockScopesIntegrationTest {
@Test
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException {
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.getTransaction()
.commit();
em.close();
// NORMAL SCOPE
em = getEntityManagerWithOpenTransaction();
PessimisticLockingEmployee foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em.getTransaction()
EntityManager em2 = getEntityManagerWithOpenTransaction();
PessimisticLockingEmployee foundEmployee = em2.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em2.getTransaction()
.rollback();
// EXTENDED SCOPE
Map<String, Object> map = new HashMap<>();
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
em = getEntityManagerWithOpenTransaction();
foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
EntityManager em3 = getEntityManagerWithOpenTransaction();
foundEmployee = em3.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
em3.getTransaction()
.rollback();
em2.close();
em3.close();
}
@Test
public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException {
public void givenEntityWithElementCollection_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
EntityManager em = getEntityManagerWithOpenTransaction();
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.getTransaction()
.commit();
em.close();
// NORMAL SCOPE
em = getEntityManagerWithOpenTransaction();
Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em.getTransaction()
EntityManager em2 = getEntityManagerWithOpenTransaction();
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em2.getTransaction()
.rollback();
// EXTENDED SCOPE
Map<String, Object> map = new HashMap<>();
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
em = getEntityManagerWithOpenTransaction();
foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
EntityManager em3 = getEntityManagerWithOpenTransaction();
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
em2.getTransaction()
.rollback();
em2.close();
em3.close();
}
@Test
public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException {
public void givenEntityWithOneToMany_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
EntityManager em = getEntityManagerWithOpenTransaction();
PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE");
PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student);
@ -74,17 +84,22 @@ public class PessimisticLockScopesIntegrationTest {
em.close();
// NORMAL SCOPE
em = getEntityManagerWithOpenTransaction();
PessimisticLockingCourse foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em.getTransaction()
EntityManager em2 = getEntityManagerWithOpenTransaction();
PessimisticLockingCourse foundCourse = em2.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em2.getTransaction()
.rollback();
// EXTENDED SCOPE
Map<String, Object> map = new HashMap<>();
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
em = getEntityManagerWithOpenTransaction();
foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
EntityManager em3 = getEntityManagerWithOpenTransaction();
foundCourse = em3.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
em3.getTransaction()
.rollback();
em2.close();
em3.close();
}
protected EntityManager getEntityManagerWithOpenTransaction() throws IOException {

View File

@ -22,54 +22,64 @@ public class PessimisticLockScopesIntegrationTest {
EntityManagerFactory entityManagerFactory;
@Test
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() {
public void givenEntityWithJoinInheritance_whenLock_thenNormalAndExtendScopesLockParentAndChildEntity() {
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.getTransaction()
.commit();
em.close();
// NORMAL SCOPE
em = getEntityManagerWithOpenTransaction();
Employee foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em.getTransaction()
EntityManager em2 = getEntityManagerWithOpenTransaction();
Employee foundEmployee = em2.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em2.getTransaction()
.rollback();
// EXTENDED SCOPE
Map<String, Object> map = new HashMap<>();
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
em = getEntityManagerWithOpenTransaction();
foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
EntityManager em3 = getEntityManagerWithOpenTransaction();
foundEmployee = em3.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
em3.getTransaction()
.rollback();
em2.close();
em3.close();
}
@Test
public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() {
public void givenEntityWithElementCollection_whenLock_thenExtendScopeLocksAlsoCollectionTable() {
EntityManager em = getEntityManagerWithOpenTransaction();
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.getTransaction()
.commit();
em.close();
// NORMAL SCOPE
em = getEntityManagerWithOpenTransaction();
Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em.getTransaction()
EntityManager em2 = getEntityManagerWithOpenTransaction();
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em2.getTransaction()
.rollback();
// EXTENDED SCOPE
Map<String, Object> map = new HashMap<>();
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
em = getEntityManagerWithOpenTransaction();
foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
EntityManager em3 = getEntityManagerWithOpenTransaction();
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
em3.getTransaction()
.rollback();
em2.close();
em3.close();
}
@Test
public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() {
public void givenEclipseEntityWithOneToMany_whenLock_thenExtendedLockAlsoJoinTable() {
EntityManager em = getEntityManagerWithOpenTransaction();
Student student = new Student(1L, "JOE");
Course course = new Course(1L, "COURSE", student);
@ -81,17 +91,22 @@ public class PessimisticLockScopesIntegrationTest {
em.close();
// NORMAL SCOPE
em = getEntityManagerWithOpenTransaction();
Course foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em.getTransaction()
EntityManager em2 = getEntityManagerWithOpenTransaction();
Course foundCourse = em2.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE);
em2.getTransaction()
.rollback();
// EXTENDED SCOPE
Map<String, Object> map = new HashMap<>();
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
em = getEntityManagerWithOpenTransaction();
foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
EntityManager em3 = getEntityManagerWithOpenTransaction();
foundCourse = em3.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
em3.getTransaction()
.rollback();
em2.close();
em3.close();
}
protected EntityManager getEntityManagerWithOpenTransaction() {