Search in sources :

Example 11 with UnitOfWorkImpl

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

the class UnitOfWorkValueHolder method instantiateImpl.

/**
 * a.k.a getValueFromWrappedValueholder.
 * The old name is no longer correct, as query based valueholders are now
 * sometimes triggered directly without triggering the underlying valueholder.
 */
protected T instantiateImpl() {
    Object value;
    // called below are not threadsafe alone.
    synchronized (this.wrappedValueHolder) {
        if (this.wrappedValueHolder instanceof DatabaseValueHolder) {
            DatabaseValueHolder<T> wrapped = (DatabaseValueHolder<T>) this.wrappedValueHolder;
            UnitOfWorkImpl unitOfWork = getUnitOfWork();
            if (!wrapped.isEasilyInstantiated()) {
                if (wrapped.isPessimisticLockingValueHolder()) {
                    if (!unitOfWork.getCommitManager().isActive() && !unitOfWork.wasTransactionBegunPrematurely()) {
                        unitOfWork.beginEarlyTransaction();
                    }
                    unitOfWork.log(SessionLog.FINEST, SessionLog.TRANSACTION, "instantiate_pl_relationship");
                }
                if (unitOfWork.getCommitManager().isActive() || unitOfWork.wasTransactionBegunPrematurely()) {
                    // UnitOfWork valueholder on the UnitOfWork only.
                    return wrapped.instantiateForUnitOfWorkValueHolder(this);
                }
            }
            if (!wrapped.isInstantiated()) {
                // if not instantiated then try and load the UOW versions to prevent the whole loading from the cache and cloning
                // process
                T result = wrapped.getValue((UnitOfWorkImpl) this.session);
                if (result != null) {
                    return result;
                }
            }
        }
        value = this.wrappedValueHolder.getValue();
    }
    return buildCloneFor(value);
}
Also used : UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 12 with UnitOfWorkImpl

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

the class DeleteProxyObjectTest method test.

@Override
public void test() {
    Employee emp = (Employee) getSession().readObject(Employee.class, new ExpressionBuilder().get("firstName").like("%Rick%"));
    address = emp.getAddress();
    UnitOfWorkImpl uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
    Employee aClone = (Employee) uow.registerObject(emp);
    aClone.setAddress(null);
    uow.deleteObject(address);
    uow.commit();
}
Also used : UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 13 with UnitOfWorkImpl

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

the class ReadOnlyClassAccessingTestCase method test.

@Override
protected void test() {
    // Test acquiring a unit of work.
    UnitOfWork uow1 = getSession().acquireUnitOfWork();
    if (!uow1.getReadOnlyClasses().isEmpty()) {
        throw new TestErrorException(" When acquiring a UnitOfWork from a Session, the read-only classes where not empty as expected.");
    }
    uow1.release();
    // Test acquiring a unit of work with a vector of read-only classes.
    Vector classes = new Vector();
    classes.addElement(Promoter.class);
    classes.addElement(Country.class);
    UnitOfWork uow2 = getSession().acquireUnitOfWork();
    uow2.removeAllReadOnlyClasses();
    uow2.addReadOnlyClasses(classes);
    if (!areEqual(uow2.getReadOnlyClasses(), classes)) {
        throw new TestErrorException("When acquiring a UnitOfWork from a Session, the read-only classes specified did not get set in the UnitOfWork;");
    }
    // Test the testing of read-only classes
    for (Enumeration enumtr = classes.elements(); enumtr.hasMoreElements(); ) {
        if (!uow2.isClassReadOnly((Class) enumtr.nextElement())) {
            throw new TestErrorException("Testing whether a class is read-only or not has failed.");
        }
    }
    if (uow2.isClassReadOnly(Vector.class)) {
        throw new TestErrorException("Testing whether a class is read-only or not has failed.");
    }
    // Test the add and remove of read-only classes.
    uow2.removeReadOnlyClass(Promoter.class);
    if (uow2.isClassReadOnly(Promoter.class)) {
        throw new TestErrorException("The method removeReadOnlyClass(Class) failed.");
    }
    uow2.addReadOnlyClass(Promoter.class);
    if (!uow2.isClassReadOnly(Promoter.class)) {
        throw new TestErrorException("The method addReadOnlyClass(Class) failed.");
    }
    // Test the removeAll.
    uow2.removeAllReadOnlyClasses();
    if ((uow2.isClassReadOnly(Country.class)) || (!uow2.getReadOnlyClasses().isEmpty())) {
        throw new TestErrorException("Did not remove all the read-only classes from a UnitOfWork properly");
    }
    // Check that we cannot make  changes to the read-only set after registering an object.
    try {
        uow2.registerObject(new Address());
        uow2.removeAllReadOnlyClasses();
        uow2.addReadOnlyClasses(classes);
        if (areEqual(uow2.getReadOnlyClasses(), classes)) {
            throw new TestErrorException("Shouldn't be able to change the readOnlyClasses of a UnitOfWork after an object was registered.");
        }
    } catch (org.eclipse.persistence.exceptions.ValidationException ex) {
        getSession().logMessage("Caught validation exeception...OK");
    } finally {
        uow2.release();
    }
    // Check that the default read-only classes work.
    Vector someClasses = new Vector();
    someClasses.addElement(Country.class);
    someClasses.addElement(Address.class);
    getSession().getProject().setDefaultReadOnlyClasses(someClasses);
    UnitOfWork uow3 = getSession().acquireUnitOfWork();
    if (!areEqual(uow3.getReadOnlyClasses(), someClasses)) {
        throw new TestErrorException("The default read-only classes were not set properly when a UnitOfWork was aquired");
    }
    // Nested units of work should not be able to reduce the set of read-only classes.
    UnitOfWork uow4 = uow3.acquireUnitOfWork();
    try {
        uow4.removeAllReadOnlyClasses();
    } catch (org.eclipse.persistence.exceptions.ValidationException ex) {
        getSession().logMessage("Check the nested units of work read-only classes. OK");
    } finally {
        uow3.release();
        uow4.release();
    }
    // You should be able to get the default set of read-only classes from nested UnitOfWork objects.
    UnitOfWork uow5 = getSession().acquireUnitOfWork();
    UnitOfWork uow6 = uow5.acquireUnitOfWork();
    if (!areEqual(((UnitOfWorkImpl) uow5).getDefaultReadOnlyClasses(), ((UnitOfWorkImpl) uow6).getDefaultReadOnlyClasses()))
        throw new TestErrorException("Nested UnitOfWorks did not return consistent default read-only classes.");
    uow5.release();
    uow6.release();
}
Also used : Promoter(org.eclipse.persistence.testing.models.readonly.Promoter) Address(org.eclipse.persistence.testing.models.readonly.Address) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 14 with UnitOfWorkImpl

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

the class UnitOfWorkExistingObjectsListTest method test.

@Override
public void test() {
    Session session = getSession();
    UnitOfWork uow = session.acquireUnitOfWork();
    Address addr = (Address) uow.readObject(Address.class);
    uow.release();
    uow = session.acquireUnitOfWork();
    uow.setValidationLevel(UnitOfWorkImpl.None);
    Employee emp = new Employee();
    uow.registerNewObject(emp);
    emp.setAddress(addr);
    uow.assignSequenceNumbers();
    if (!((UnitOfWorkImpl) uow).getUnregisteredExistingObjects().containsKey(addr)) {
        throw new TestErrorException(" Bug 294259 -  Duplicate existence checks in same UOW.  Patch Failed");
    }
    uow.release();
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) Address(org.eclipse.persistence.testing.models.employee.domain.Address) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) Session(org.eclipse.persistence.sessions.Session)

Example 15 with UnitOfWorkImpl

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

the class UnitOfWorkCommitToDatabaseTest method test.

@Override
protected void test() {
    try {
        // Test object
        ObjectA testObject = ObjectA.example3();
        testObject.setName(OBJECT_NAME);
        UnitOfWorkImpl uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
        // Commit test object to DB using commitToDatabase()
        uow.registerObject(testObject);
        uow.commitRootUnitOfWork();
    } catch (Exception e) {
        e.printStackTrace();
        setStoredException(new TestErrorException("Error calling commitToDatabase() : " + TEST_NAME));
        return;
    }
}
Also used : TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) ObjectA(org.eclipse.persistence.testing.models.ownership.ObjectA) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException)

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