Search in sources :

Example 1 with RepeatableWriteUnitOfWork

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

the class AdvancedJunitTest method testEL254937.

public void testEL254937() {
    // Should not run in the server - bug 264589
    if (!isOnServer()) {
        EntityManager em = createEntityManager();
        beginTransaction(em);
        LargeProject lp1 = new LargeProject();
        lp1.setName("one");
        em.persist(lp1);
        commitTransaction(em);
        em = createEntityManager();
        beginTransaction(em);
        em.remove(em.find(LargeProject.class, lp1.getId()));
        em.flush();
        JpaEntityManager eclipselinkEm = (JpaEntityManager) em.getDelegate();
        RepeatableWriteUnitOfWork uow = (RepeatableWriteUnitOfWork) eclipselinkEm.getActiveSession();
        // duplicate the beforeCompletion call
        uow.issueSQLbeforeCompletion();
        // commit the transaction
        uow.setShouldTerminateTransaction(true);
        uow.commitTransaction();
        // duplicate the AfterCompletion call.  This should merge, removing the LargeProject from the shared cache
        uow.mergeClonesAfterCompletion();
        em = createEntityManager();
        LargeProject cachedLargeProject = em.find(LargeProject.class, lp1.getId());
        closeEntityManager(em);
        assertTrue("Entity removed during flush was not removed from the shared cache on commit", cachedLargeProject == null);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 2 with RepeatableWriteUnitOfWork

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

the class EntityManagerJUnitTestSuite method testNoPersistOnCommit.

// Test Not using the persist operation on commit.
public void testNoPersistOnCommit() {
    // Properties only works in jse.
    if (isOnServer()) {
        return;
    }
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Employee employee = new Employee();
    employee.setLastName("SomeName");
    Address addr = new Address();
    addr.setCity("Douglas");
    try {
        em.persist(employee);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager();
        beginTransaction(em);
        ((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
        employee = em.find(Employee.class, employee.getId());
        employee.setAddress(addr);
        addr.getEmployees().add(employee);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager();
        clearCache();
        beginTransaction(em);
        ((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
        employee = em.find(Employee.class, employee.getId());
        employee.getAddress().setCountry("country");
        employee.getAddress().getEmployees().size();
        employee.setAddress((Address) null);
        em.remove(employee);
        commitTransaction(em);
        em = createEntityManager();
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        assertNull("Employee Not Deleted", employee);
        commitTransaction(em);
        closeEntityManager(em);
    } catch (RuntimeException exception) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw exception;
    } finally {
        try {
            em = createEntityManager();
            clearCache();
            beginTransaction(em);
            em.remove(em.find(Address.class, addr.getID()));
            commitTransaction(em);
        } catch (RuntimeException ex) {
        // ignore
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork)

Example 3 with RepeatableWriteUnitOfWork

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

the class RelationshipModelJUnitTestSuite method testChangeSetForNewObject.

// Bug 357103
public void testChangeSetForNewObject() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Customer cust = new Customer();
    cust.setName("Joe");
    em.persist(cust);
    Order order = new Order();
    order.setQuantity(1);
    em.persist(order);
    cust.addOrder(order);
    commitTransaction(em);
    RepeatableWriteUnitOfWork uow = null;
    try {
        beginTransaction(em);
        cust = em.find(Customer.class, cust.getCustomerId());
        Order order2 = new Order();
        order2.setQuantity(2);
        order2.setShippingAddress("123 Main St.");
        em.persist(order2);
        cust.addOrder(order2);
        EntityManagerImpl impl = (EntityManagerImpl) JpaHelper.getEntityManager(em);
        uow = impl.getActivePersistenceContext(null);
        em.flush();
        UnitOfWorkChangeSet uowChangeSet = uow.getCumulativeUOWChangeSet();
        ObjectChangeSet customerChangeSet = uowChangeSet.getCloneToObjectChangeSet().get(cust);
        CollectionChangeRecord orderChangeRecord = (CollectionChangeRecord) customerChangeSet.getChangesForAttributeNamed("orders");
        Iterator<ObjectChangeSet> i = orderChangeRecord.getAddObjectList().keySet().iterator();
        while (i.hasNext()) {
            ObjectChangeSet orderChangeSet = i.next();
            assertTrue("There are changes in the change set.  There should be no changes for a new object.", orderChangeSet.getChanges().isEmpty());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        rollbackTransaction(em);
        beginTransaction(em);
        cust = em.find(Customer.class, cust.getCustomerId());
        em.remove(cust);
        commitTransaction(em);
    }
}
Also used : Order(org.eclipse.persistence.testing.models.jpa.relationships.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.eclipse.persistence.testing.models.jpa.relationships.Customer) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork) CollectionChangeRecord(org.eclipse.persistence.internal.sessions.CollectionChangeRecord) PersistenceException(jakarta.persistence.PersistenceException) QueryException(org.eclipse.persistence.exceptions.QueryException) NonUniqueResultException(jakarta.persistence.NonUniqueResultException) NoResultException(jakarta.persistence.NoResultException)

Example 4 with RepeatableWriteUnitOfWork

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

the class EntityManagerJUnitTestSuite method testNoPersistOnCommit.

// Test Not using the persist operation on commit.
public void testNoPersistOnCommit() {
    // Properties only works in jse.
    if (isOnServer()) {
        return;
    }
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Employee employee = new Employee();
    employee.setLastName("SomeName");
    Address addr = new Address();
    addr.setCity("Douglas");
    try {
        em.persist(employee);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager();
        beginTransaction(em);
        ((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
        employee = em.find(Employee.class, employee.getId());
        employee.setAddress(addr);
        addr.getEmployees().add(employee);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(employee);
        closeEntityManager(em);
        em = createEntityManager();
        clearCache();
        beginTransaction(em);
        ((RepeatableWriteUnitOfWork) JpaHelper.getEntityManager(em).getUnitOfWork()).setDiscoverUnregisteredNewObjectsWithoutPersist(true);
        employee = em.find(Employee.class, employee.getId());
        employee.getAddress().setCountry("country");
        employee.getAddress().getEmployees().size();
        employee.setAddress((Address) null);
        em.remove(employee);
        commitTransaction(em);
        em = createEntityManager();
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        assertNull("Employee Not Deleted", employee);
        commitTransaction(em);
        closeEntityManager(em);
    } catch (RuntimeException exception) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw exception;
    } finally {
        try {
            em = createEntityManager();
            clearCache();
            beginTransaction(em);
            em.remove(em.find(Address.class, addr.getID()));
            commitTransaction(em);
        } catch (RuntimeException ex) {
        // ignore
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork)

Example 5 with RepeatableWriteUnitOfWork

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

the class IsolatedCacheTestSuite method testCacheIsolationDBQueryHit.

public void testCacheIsolationDBQueryHit() throws Exception {
    EntityManager em = createEntityManager("fieldaccess");
    // Step 1 - get an isolated item in the cache.
    beginTransaction(em);
    try {
        IsolatedItem item = new IsolatedItem();
        item.setDescription("A phoney item");
        item.setName("Phoney name");
        em.persist(item);
        commitTransaction(em);
        // Step 2 - clear the entity manager and see if the item still exists
        // in the uow cache.
        beginTransaction(em);
        em.clear();
        RepeatableWriteUnitOfWork uow = (RepeatableWriteUnitOfWork) ((EntityManagerImpl) em.getDelegate()).getUnitOfWork();
        assertFalse("The isolated item was not cleared from the shared cache", uow.getIdentityMapAccessor().containsObjectInIdentityMap(item));
        assertFalse("The isolated item was not cleared from the uow cache", uow.getParent().getIdentityMapAccessor().containsObjectInIdentityMap(item));
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork)

Aggregations

RepeatableWriteUnitOfWork (org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork)11 EntityManager (jakarta.persistence.EntityManager)5 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)4 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)3 PersistenceException (jakarta.persistence.PersistenceException)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DescriptorEvent (org.eclipse.persistence.descriptors.DescriptorEvent)2 VersionLockingPolicy (org.eclipse.persistence.descriptors.VersionLockingPolicy)2 OptimisticLockingPolicy (org.eclipse.persistence.internal.descriptors.OptimisticLockingPolicy)2 UnitOfWorkChangeSet (org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet)2 WriteObjectQuery (org.eclipse.persistence.queries.WriteObjectQuery)2 LockTimeoutException (jakarta.persistence.LockTimeoutException)1 NoResultException (jakarta.persistence.NoResultException)1 NonUniqueResultException (jakarta.persistence.NonUniqueResultException)1 DescriptorEventManager (org.eclipse.persistence.descriptors.DescriptorEventManager)1 DescriptorQueryManager (org.eclipse.persistence.descriptors.DescriptorQueryManager)1 QueryException (org.eclipse.persistence.exceptions.QueryException)1 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 CollectionChangeRecord (org.eclipse.persistence.internal.sessions.CollectionChangeRecord)1