Search in sources :

Example 36 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.

the class AbstractTransactionController method lookupActiveUnitOfWork.

/**
 * INTERNAL:
 * Return the unit of work associated with the active external transaction.
 * Return null if no transaction is active, or if no uow has been associated with
 * the active transaction yet.
 */
public UnitOfWorkImpl lookupActiveUnitOfWork(Object transaction) {
    if (transaction == null) {
        return null;
    }
    Object transactionKey = getTransactionKey(transaction);
    // PERF: Cache the active unit of work in a thread local.
    // This is just a heuristic, so uses == and no tx-key and direct access as extremely high throughput.
    UnitOfWorkImpl activeUnitOfWork = this.activeUnitOfWorkThreadLocal.get();
    if (activeUnitOfWork != null) {
        if (transaction == activeUnitOfWork.getTransaction()) {
            return activeUnitOfWork;
        }
    }
    activeUnitOfWork = getUnitsOfWork().get(transactionKey);
    if (activeUnitOfWork != null) {
        activeUnitOfWork.setTransaction(transaction);
    }
    this.activeUnitOfWorkThreadLocal.set(activeUnitOfWork);
    return activeUnitOfWork;
}
Also used : UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 37 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.

the class ConcurrencyTest method testTransitionToDeferedFailure.

/**
 * The setup is done as a test, both to record its failure, and to allow
 * execution in the server.
 */
public void testTransitionToDeferedFailure() {
    if (isOnServer()) {
        return;
    }
    Integer toWaitOn = 4;
    Thread thread1 = null;
    EntityManagerFactory emf = getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    ConcurrencyA a = new ConcurrencyA();
    ConcurrencyA a2 = new ConcurrencyA();
    ConcurrencyB b = new ConcurrencyB();
    ConcurrencyC c = new ConcurrencyC();
    em.persist(a);
    em.persist(a2);
    em.persist(c);
    em.persist(b);
    em.getTransaction().commit();
    em.close();
    try {
        thread1 = new Thread(new TransitionRunner1(toWaitOn, b, c, emf));
        thread1.start();
        em = emf.createEntityManager();
        a = em.find(ConcurrencyA.class, a.getId());
        a2 = em.find(ConcurrencyA.class, a2.getId());
        b = em.find(ConcurrencyB.class, b.getId());
        c = em.find(ConcurrencyC.class, c.getId());
        a2.setName(System.currentTimeMillis() + "_A");
        a.setConcurrencyB(b);
        a.setConcurrencyC(c);
        UnitOfWorkImpl uow = ((EntityManagerImpl) em).getActivePersistenceContext(null);
        try {
            Thread.sleep(20000);
            synchronized (toWaitOn) {
                toWaitOn.notifyAll();
                toWaitOn.wait();
            }
        } catch (InterruptedException e) {
        }
        uow.issueSQLbeforeCompletion(true);
        uow.mergeClonesAfterCompletion();
        synchronized (toWaitOn) {
            toWaitOn.notifyAll();
        }
        try {
            thread1.join(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } finally {
        if (thread1.isAlive()) {
            thread1.interrupt();
        }
        em = emf.createEntityManager();
        em.getTransaction().begin();
        em.remove(em.find(ConcurrencyA.class, a.getId()));
        em.remove(em.find(ConcurrencyA.class, a2.getId()));
        em.remove(em.find(ConcurrencyC.class, c.getId()));
        em.remove(em.find(ConcurrencyB.class, b.getId()));
        em.getTransaction().commit();
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) ConcurrencyA(org.eclipse.persistence.testing.models.jpa.advanced.ConcurrencyA) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) ConcurrencyB(org.eclipse.persistence.testing.models.jpa.advanced.ConcurrencyB) ConcurrencyC(org.eclipse.persistence.testing.models.jpa.advanced.ConcurrencyC) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 38 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl 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 39 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl 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 40 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl 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)

Aggregations

UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)92 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)21 TestErrorException (org.eclipse.persistence.testing.framework.TestErrorException)19 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)18 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)15 EntityManager (jakarta.persistence.EntityManager)14 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)11 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)9 Vector (java.util.Vector)8 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)8 Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)8 CacheKey (org.eclipse.persistence.internal.identitymaps.CacheKey)7 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)7 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)7 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)6 UnitOfWorkChangeSet (org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet)6 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)5 BigDecimal (java.math.BigDecimal)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)5 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)5