Search in sources :

Example 31 with Department

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);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 32 with Department

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) {
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) CacheKey(org.eclipse.persistence.internal.identitymaps.CacheKey)

Aggregations

Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)32 EntityManager (jakarta.persistence.EntityManager)31 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)23 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)10 Equipment (org.eclipse.persistence.testing.models.jpa.advanced.Equipment)10 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)8 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)6 PersistenceException (jakarta.persistence.PersistenceException)6 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)6 EntityExistsException (jakarta.persistence.EntityExistsException)5 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)5 NoResultException (jakarta.persistence.NoResultException)5 OptimisticLockException (jakarta.persistence.OptimisticLockException)5 Query (jakarta.persistence.Query)5 RollbackException (jakarta.persistence.RollbackException)5 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)5 SQLException (java.sql.SQLException)5 HashMap (java.util.HashMap)5 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)5 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)5