use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class LifecycleJUnitTest method testClearWhileEntityManagerInCommitPendingStateWithClearAfterCommit.
/**
* This test simulates EE container callbacks that could occur that affect em lifecycle state.
* Specifically it tests whether we handle an attempt to clear an entityManager
* that is in the middle of a commit.
* We only clear the entityManager if we are in the states
* (Birth == 0, WriteChangesFailed==3, Death==5 or AfterExternalTransactionRolledBack==6).
* If we are in one of the following *Pending states we defer the clear() to the release() call later
*/
public void testClearWhileEntityManagerInCommitPendingStateWithClearAfterCommit() {
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
// 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();
// clear em
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 Runner1 method run.
@Override
public void run() {
try {
EntityManager em = emf.createEntityManager();
Department dept = em.find(Department.class, deptPK);
Equipment equip = em.find(Equipment.class, equipPK);
dept.getEquipment().put(equip.getId(), equip);
UnitOfWorkImpl uow = ((EntityManagerImpl) em).getActivePersistenceContext(null);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
uow.issueSQLbeforeCompletion(true);
synchronized (this.waitOn) {
try {
this.waitOn.notify();
this.waitOn.wait();
} catch (InterruptedException e) {
}
}
CacheKey cacheKey = uow.getParent().getParent().getIdentityMapAccessorInstance().getCacheKeyForObject(dept);
synchronized (cacheKey) {
cacheKey.notify();
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
uow.mergeClonesAfterCompletion();
} catch (Exception ex) {
}
}
Aggregations