Search in sources :

Example 16 with UnitOfWorkImpl

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

the class WasTransactionBegunPrematurelyRollbackTest method test.

@Override
public void test() {
    Person person = new Person();
    UnitOfWork uow = getSession().acquireUnitOfWork();
    Person clonePerson = (Person) uow.registerObject(person);
    clonePerson.id = id;
    getAbstractSession().beginTransaction();
    ((UnitOfWorkImpl) uow).setWasTransactionBegunPrematurely(true);
    try {
        uow.commit();
    } catch (Exception ex) {
        exception = ex;
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) TestWarningException(org.eclipse.persistence.testing.framework.TestWarningException)

Example 17 with UnitOfWorkImpl

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

the class ForceWeakReferenceTest method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork(ReferenceMode.FORCE_WEAK);
    int size = uow.readAllObjects(Employee.class).size();
    try {
        Long[] arr = new Long[10000000];
        for (int i = 0; i < 10000000; ++i) {
            arr[i] = (long) i;
        }
        System.gc();
        try {
            Thread.sleep(200);
        } catch (InterruptedException ex) {
        }
        System.gc();
    } catch (Error er) {
    // ignore
    }
    if (((UnitOfWorkImpl) uow).getCloneMapping().size() == size) {
        throw new TestErrorException("Did not release forced weak references.");
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 18 with UnitOfWorkImpl

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

the class UnregisterUnitOfWorkTest method test.

@Override
protected void test() {
    /**
     ****************
     */
    /* DEEP UNREGISTER */
    /**
     ****************
     */
    Employee employee = (org.eclipse.persistence.testing.models.employee.domain.Employee) (new org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator()).employeeExample1();
    UnitOfWorkImpl uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
    Employee workingCopy = (Employee) uow.registerObject(employee);
    workingCopy.setFirstName("firstName");
    uow.deepUnregisterObject(workingCopy);
    if (!uow.getCloneMapping().isEmpty()) {
        throw new TestErrorException("Deep unregister object did not work");
    }
    if (!uow.getNewObjectsOriginalToClone().isEmpty()) {
        throw new TestErrorException("Deep unregister object did not work");
    }
    if (!uow.getNewObjectsCloneToOriginal().isEmpty()) {
        throw new TestErrorException("Deep unregister object did not work");
    }
    uow.commit();
    uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
    Vector workingCopies = uow.readAllObjects(Employee.class);
    for (Enumeration enumtr = workingCopies.elements(); enumtr.hasMoreElements(); ) {
        uow.deepUnregisterObject(enumtr.nextElement());
    }
    if (!uow.getCloneMapping().isEmpty()) {
        throw new TestErrorException("Deep unregister object did not work");
    }
    if (!uow.getNewObjectsOriginalToClone().isEmpty()) {
        throw new TestErrorException("Deep unregister object did not work");
    }
    if (!uow.getNewObjectsCloneToOriginal().isEmpty()) {
        throw new TestErrorException("Deep unregister object did not work");
    }
    uow.commit();
    /**
     ****************
     */
    /*  UNREGISTER     */
    /**
     ****************
     */
    uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
    workingCopy = (Employee) uow.registerObject(employee);
    workingCopy.getAddress();
    workingCopy.getProjects();
    workingCopy.setFirstName("firstName");
    uow.unregisterObject(workingCopy);
    Employee newWorkingCopy = (Employee) uow.registerObject(employee);
    if (workingCopy == newWorkingCopy) {
        throw new TestErrorException("Cascade private parts unregister object did not work");
    }
    if (workingCopy.getAddress() == newWorkingCopy.getAddress()) {
        throw new TestErrorException("Cascade private parts unregister object did not work");
    }
    for (Enumeration enumtr = workingCopy.getProjects().elements(); enumtr.hasMoreElements(); ) {
        if (!newWorkingCopy.getProjects().contains(enumtr.nextElement())) {
            throw new TestErrorException("Cascade private parts unregister object did not work");
        }
    }
    uow.commit();
    /**
     *******************
     */
    /* SHALLOW UNREGISTER */
    /**
     *******************
     */
    uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
    workingCopy = (Employee) uow.registerObject(employee);
    workingCopy.getAddress();
    workingCopy.setFirstName("firstName");
    uow.shallowUnregisterObject(workingCopy);
    newWorkingCopy = (Employee) uow.registerObject(employee);
    if (workingCopy == newWorkingCopy) {
        throw new TestErrorException("Shallow unregister object did not work");
    }
    if (workingCopy.getAddress() != newWorkingCopy.getAddress()) {
        throw new TestErrorException("Shallow unregister object did not work");
    }
    uow.commit();
}
Also used : Enumeration(java.util.Enumeration) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) Vector(java.util.Vector)

Example 19 with UnitOfWorkImpl

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

the class HardReferenceTest method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    int size = uow.readAllObjects(ALCTEmployee.class).size();
    try {
        Long[] arr = new Long[100000];
        for (int i = 0; i < 100000; ++i) {
            arr[i] = (long) i;
        }
    } catch (Error er) {
    // ignore
    }
    if (((UnitOfWorkImpl) uow).getCloneMapping().size() != size) {
        throw new TestErrorException("Did not hold onto all references.");
    }
}
Also used : ALCTEmployee(org.eclipse.persistence.testing.tests.unitofwork.changeflag.model.ALCTEmployee) UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 20 with UnitOfWorkImpl

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

the class ObjectBuilder method buildWorkingCopyCloneNormally.

/**
 * buildWorkingCopyCloneFromRow is an alternative to this which is the
 * normal behavior.
 * A row is read from the database, an original is built/refreshed/returned
 * from the shared cache, and the original is registered/conformed/reverted
 * in the UnitOfWork.
 * <p>
 * This default behavior is only safe when the query is executed on a read
 * connection, otherwise uncommitted data might get loaded into the shared
 * cache.
 * <p>
 * Represents the way TopLink has always worked.
 */
protected Object buildWorkingCopyCloneNormally(ObjectBuildingQuery query, AbstractRecord databaseRow, UnitOfWorkImpl unitOfWork, Object primaryKey, CacheKey preFetchedCacheKey, ClassDescriptor concreteDescriptor, JoinedAttributeManager joinManager) throws DatabaseException, QueryException {
    // First check local unit of work cache.
    CacheKey unitOfWorkCacheKey = unitOfWork.getIdentityMapAccessorInstance().acquireLock(primaryKey, concreteDescriptor.getJavaClass(), concreteDescriptor, query.isCacheCheckComplete());
    Object clone = unitOfWorkCacheKey.getObject();
    boolean found = clone != null;
    Object original = null;
    try {
        // Only check parent cache if not in unit of work, or if a refresh is required.
        if (!found || query.shouldRefreshIdentityMapResult() || // Need to build original to cache it.
        query.shouldCacheQueryResults() || query.shouldRetrieveBypassCache() || (concreteDescriptor.hasFetchGroupManager() && concreteDescriptor.getFetchGroupManager().isPartialObject(clone))) {
            // This is normal case when we are not in transaction.
            // Pass the query off to the parent.  Let it build the object and
            // cache it normally, then register/refresh it.
            AbstractSession session = unitOfWork.getParentIdentityMapSession(query);
            // forwarding queries to different sessions is now as simple as setting
            // the session on the query.
            query.setSession(session);
            if (session.isUnitOfWork()) {
                // If a nested unit of work, recurse.
                original = buildObjectInUnitOfWork(query, joinManager, databaseRow, (UnitOfWorkImpl) session, primaryKey, preFetchedCacheKey, concreteDescriptor);
                // GFBug#404  Pass in joinManager or not based on if shouldCascadeCloneToJoinedRelationship is set to true
                if (unitOfWork.shouldCascadeCloneToJoinedRelationship()) {
                    return query.registerIndividualResult(original, primaryKey, unitOfWork, joinManager, concreteDescriptor);
                } else {
                    return query.registerIndividualResult(original, primaryKey, unitOfWork, null, concreteDescriptor);
                }
            } else {
                // PERF: This optimizes the normal case to avoid duplicate cache access.
                CacheKey parentCacheKey = (CacheKey) buildObject(true, query, databaseRow, session, primaryKey, preFetchedCacheKey, concreteDescriptor, joinManager);
                original = parentCacheKey.getObject();
                if (query.shouldCacheQueryResults()) {
                    query.cacheResult(original);
                }
                // PERF: Do not register nor process read-only.
                if (unitOfWork.isClassReadOnly(original.getClass(), concreteDescriptor)) {
                    // There is an obscure case where they object could be read-only and pessimistic.
                    // Record clone if referenced class has pessimistic locking policy.
                    query.recordCloneForPessimisticLocking(original, unitOfWork);
                    return original;
                }
                if (!query.isRegisteringResults()) {
                    return original;
                }
                if (clone == null) {
                    clone = unitOfWork.cloneAndRegisterObject(original, parentCacheKey, unitOfWorkCacheKey, concreteDescriptor);
                // TODO-dclarke: At this point the clones do not have their fetch-group specified
                // relationship attributes loaded
                }
                // fetch group manager control fetch group support
                if (concreteDescriptor.hasFetchGroupManager()) {
                    // if the object is already registered in uow, but it's partially fetched (fetch group case)
                    if (concreteDescriptor.getFetchGroupManager().shouldWriteInto(original, clone)) {
                        // there might be cases when reverting/refreshing clone is needed.
                        concreteDescriptor.getFetchGroupManager().writePartialIntoClones(original, clone, unitOfWork.getBackupClone(clone, concreteDescriptor), unitOfWork);
                    }
                }
            }
        }
        query.postRegisterIndividualResult(clone, original, primaryKey, unitOfWork, joinManager, concreteDescriptor);
    } finally {
        unitOfWorkCacheKey.release();
        query.setSession(unitOfWork);
    }
    return clone;
}
Also used : UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) CacheKey(org.eclipse.persistence.internal.identitymaps.CacheKey) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

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