Search in sources :

Example 1 with Department

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

Example 2 with Department

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);
    }
}
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) Map(java.util.Map)

Example 3 with Department

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);
    }
}
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) Map(java.util.Map)

Example 4 with Department

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);
    }
}
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 5 with Department

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);
    }
}
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)

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