use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class ConcurrencyTest method testDeadLockOnReadLock.
/**
* The setup is done as a test, both to record its failure, and to allow
* execution in the server.
*/
public void testDeadLockOnReadLock() {
if (isOnServer()) {
return;
}
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Department dept = new Department();
em.persist(dept);
Equipment equip = new Equipment();
em.persist(equip);
em.getTransaction().commit();
em.close();
try {
Integer i = 5;
Thread thread1 = new Thread(new Runner1(i, dept.getId(), equip.getId(), emf));
thread1.setName("Runner1");
Thread thread2 = new Thread(new Runner2(i, dept.getId(), equip.getId(), emf));
thread2.setName("Runner2");
thread2.start();
thread1.start();
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (i) {
i.notifyAll();
}
try {
thread2.join(30000);
thread1.join(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (thread2.isAlive() || thread1.isAlive()) {
thread2.interrupt();
thread1.interrupt();
fail("Dead-lock occurred");
}
} finally {
em = emf.createEntityManager();
em.getTransaction().begin();
em.remove(em.find(Department.class, dept.getId()));
em.remove(em.find(Equipment.class, equip.getId()));
em.getTransaction().commit();
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class LifecycleJUnitTest method testClearWhileEntityManagerInFakeBirthState0.
public void testClearWhileEntityManagerInFakeBirthState0() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = null;
UnitOfWorkImpl uow = null;
Map cloneToOriginalsMap = null;
Department dept = null;
try {
em = emf.createEntityManager();
// get the underlying uow
uow = getUnitOfWorkFromEntityManager(em);
// force a get on the map to lazy initialize an empty map
cloneToOriginalsMap = uow.getCloneToOriginals();
// verify size 0
// we don't have access to the protected function uow.hasCloneToOriginals();
assertEquals("cloneToOriginalsMap must be size 0", 0, cloneToOriginalsMap.size());
// Verify that cloneToOriginals is null and not lazy initialized
dept = new Department();
cloneToOriginalsMap.put(null, dept);
// verify size 1
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size());
// verify we are in birth state
int lifecycleBefore = uow.getLifecycle();
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore);
// simulate a clear() call in the middle of a merge
em.clear();
// verify that the uow ignored the clear call
int lifecycleAfter = uow.getLifecycle();
assertEquals("Unchanged Birth state 0 is not set ", 0, lifecycleAfter);
// verify that a map previously set on the em was not cleared to null by the clear
// verify size 0
cloneToOriginalsMap = uow.getCloneToOriginals();
assertNotNull("cloneToOriginals Map must not be null after a clear in Birth state", cloneToOriginalsMap);
assertEquals("cloneToOriginalsMap must be not be cleared to size 0", 1, cloneToOriginalsMap.size());
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
} finally {
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class LifecycleJUnitTest method testClearWhileEntityManagerInFakeMergePendingState4.
// This test is a pure unit test that directly sets and tries to clear the uow state
// There are no actual entities managed in this example
public void testClearWhileEntityManagerInFakeMergePendingState4() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = null;
UnitOfWorkImpl uow = null;
Map cloneToOriginalsMap = null;
Department dept = null;
try {
em = emf.createEntityManager();
// get the underlying uow
uow = getUnitOfWorkFromEntityManager(em);
// force a get on the map to lazy initialize an empty map
cloneToOriginalsMap = uow.getCloneToOriginals();
// verify size 0
// we don't have access to the protected function uow.hasCloneToOriginals();
assertEquals("cloneToOriginalsMap must be size 0", 0, cloneToOriginalsMap.size());
// Verify that cloneToOriginals is null and not lazy initialized
dept = new Department();
cloneToOriginalsMap.put(null, dept);
// verify size 1
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size());
// verify we are in birth state
int lifecycleBefore = uow.getLifecycle();
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore);
// setup the uow in a simulated state
// set state to 4 = MergePending
uow.setPendingMerge();
// (via backdoor function) verify we are in PendingMerge state
int lifecycleInMerge = uow.getLifecycle();
assertEquals("MergePending state 4 is not set ", 4, lifecycleInMerge);
// simulate a clear() call in the middle of a merge
// 326097: This assertion used to be ignored by a catch block - it is a valid test but we want to clear on the EM not the UOW
em.clear();
// verify that the uow ignored the clear call
int lifecycleAfter = uow.getLifecycle();
assertEquals("UnModified MergePending state 4 should still be 4 and not Birth state 0 after a clear() ", 4, lifecycleAfter);
// verify that a map previously set on the uow was cleared to null by the
// verify size 0
assertNotNull("cloneToOriginals Map must not be null after a clear in *Pending state", cloneToOriginalsMap);
assertEquals("cloneToOriginalsMap must be size 1", 1, cloneToOriginalsMap.size());
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
} finally {
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class LifecycleJUnitTest method testClearWhileEntityManagerInCommitPendingStateWithNoClearAfterCommit.
public void testClearWhileEntityManagerInCommitPendingStateWithNoClearAfterCommit() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
Department dept = null;
try {
em.getTransaction().begin();
dept = new Department();
// A merge will not populate the @Id field and will result in a PK null exception in any find later
// A persist will populate the @Id field
em.persist(dept);
// simulate an attempt to call close() while we are in the middle of a commit
UnitOfWorkImpl uow = getUnitOfWorkFromEntityManager(em);
// get lifecycle state
int lifecycleBefore = uow.getLifecycle();
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore);
em.clear();
int lifecycleAfter = uow.getLifecycle();
assertEquals("Birth state 0 is not set after a clear on state Birth ", 0, lifecycleAfter);
em.getTransaction().commit();
// don't clear em - leave following line commented
// em.clear();
int lifecycleAfterCommit = uow.getLifecycle();
assertEquals("Birth state 0 is not set after commit ", 0, lifecycleAfterCommit);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
} finally {
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class LifecycleJUnitTest method testClearAfterEntityManagerCommitFinished.
// This clear should pass because the state is always 0 Begin except for inside the commit()
public void testClearAfterEntityManagerCommitFinished() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
Department dept = null;
try {
em.getTransaction().begin();
dept = new Department();
em.persist(dept);
// simulate an attempt to call close() while we are in the middle of a commit
UnitOfWorkImpl uow = getUnitOfWorkFromEntityManager(em);
// get lifecycle state
int lifecycleBefore = uow.getLifecycle();
assertEquals("Birth state 0 is not set ", 0, lifecycleBefore);
em.clear();
int lifecycleAfter = uow.getLifecycle();
assertEquals("Birth state 0 is not set after a clear on state Birth ", 0, lifecycleAfter);
em.getTransaction().commit();
em.clear();
int lifecycleAfterCommit = uow.getLifecycle();
assertEquals("Birth state 0 is not set after commit ", 0, lifecycleAfterCommit);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
} finally {
closeEntityManager(em);
}
}
Aggregations