Search in sources :

Example 41 with UnitOfWorkImpl

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

Example 42 with UnitOfWorkImpl

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

the class Runner2 method run.

@Override
public void run() {
    try {
        EntityManager em = emf.createEntityManager();
        Equipment equip = em.find(Equipment.class, equipPK);
        equip.setDescription("To changed");
        Department dept = em.find(Department.class, deptPK);
        dept.setName("Name Change As Well");
        UnitOfWorkImpl uow = ((EntityManagerImpl) em).getActivePersistenceContext(null);
        synchronized (this.waitOn) {
            try {
                this.waitOn.wait();
            } catch (InterruptedException e) {
            }
        }
        uow.issueSQLbeforeCompletion(true);
        uow.mergeClonesAfterCompletion();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
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)

Example 43 with UnitOfWorkImpl

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

the class TransitionRunner1 method run.

@Override
public void run() {
    EntityManager em = emf.createEntityManager();
    ConcurrencyB b = em.find(ConcurrencyB.class, concB.getId());
    ConcurrencyC c = em.find(ConcurrencyC.class, concC.getId());
    c.setName(System.currentTimeMillis() + "_C");
    b.setName(System.currentTimeMillis() + "_B");
    UnitOfWorkImpl uow = ((EntityManagerImpl) em).getActivePersistenceContext(null);
    try {
        synchronized (toWaitOn) {
            toWaitOn.wait(120000);
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    uow.issueSQLbeforeCompletion(true);
    try {
        synchronized (toWaitOn) {
            toWaitOn.notifyAll();
            toWaitOn.wait(6000);
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    uow.release();
}
Also used : EntityManager(jakarta.persistence.EntityManager) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) 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 44 with UnitOfWorkImpl

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

the class TransitionRunner2 method run.

@Override
public void run() {
    try {
        EntityManager em = emf.createEntityManager();
        Equipment equip = em.find(Equipment.class, equipPK);
        equip.setDescription("To changed");
        Department dept = em.find(Department.class, deptPK);
        dept.setName("Name Change As Well");
        UnitOfWorkImpl uow = ((EntityManagerImpl) em).getActivePersistenceContext(null);
        synchronized (this.waitOn) {
            try {
                this.waitOn.wait();
            } catch (InterruptedException e) {
            }
        }
        uow.issueSQLbeforeCompletion(true);
        uow.mergeClonesAfterCompletion();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
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)

Example 45 with UnitOfWorkImpl

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

the class EntityManagerJUnitTestSuite method testDetachRemovedObject.

// detach(object) on a removed object does not throw exception. This test only
// checks whether an removed object is completely deleted from the
// getDeletedObject()Map after 'detach(removedobject)' is invoked
public void testDetachRemovedObject() {
    // create an Employee
    Employee emp = new Employee();
    emp.setFirstName("testDetachRemovedObjectEmployee");
    // persist the Employee
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        em.persist(emp);
        commitTransaction(em);
    } catch (RuntimeException re) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw re;
    }
    beginTransaction(em);
    // attempt to remove the Employee
    em.remove(em.find(Employee.class, emp.getId()));
    commitTransaction(em);
    beginTransaction(em);
    EntityManagerImpl em1 = (EntityManagerImpl) em.getDelegate();
    try {
        // attempt to detach the Employee
        em.detach(emp);
        UnitOfWork uow = em1.getUnitOfWork();
        UnitOfWorkImpl uowImpl = (UnitOfWorkImpl) uow;
        boolean afterClear = uowImpl.getDeletedObjects().containsKey(emp);
        assertFalse("exception thrown when detaching a removed entity is attempted.", afterClear);
    } catch (IllegalArgumentException iae) {
        return;
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) 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