Search in sources :

Example 6 with UnitOfWorkImpl

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

the class NestedUnitOfWorkDeleteConformedNestedNewObjectTest method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    UnitOfWork nestedUow1 = uow.acquireUnitOfWork();
    Employee employee = (Employee) new EmployeePopulator().basicEmployeeExample1();
    employee.setId(new BigDecimal(15));
    nestedUow1.registerObject(employee);
    nestedUow1.commit();
    nestedUow1 = uow.acquireUnitOfWork();
    ReadAllQuery query = new ReadAllQuery();
    query.setReferenceClass(Employee.class);
    query.setSelectionCriteria(new org.eclipse.persistence.expressions.ExpressionBuilder().get("id").equal(new BigDecimal(15)));
    query.conformResultsInUnitOfWork();
    Vector results = (Vector) nestedUow1.executeQuery(query);
    if (results.size() > 1) {
        throw new TestErrorException("The read all query on primary key returned more than one object see bug # 3183379");
    }
    Employee nestedEmployee = (Employee) results.firstElement();
    nestedUow1.deleteObject(nestedEmployee);
    nestedUow1.commit();
    if (!((UnitOfWorkImpl) uow).getNewObjectsCloneToOriginal().isEmpty()) {
        throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) EmployeePopulator(org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) Vector(java.util.Vector) BigDecimal(java.math.BigDecimal)

Example 7 with UnitOfWorkImpl

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

the class NewObjectIdentityTest method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    origPerson = new Employee();
    Employee person = (Employee) uow.registerObject(origPerson);
    person.setFirstName("Anthony");
    person.setLastName("Anthony");
    ((UnitOfWorkImpl) uow).issueSQLbeforeCompletion();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            ReadAllQuery query = new ReadAllQuery(Employee.class);
            query.setSelectionCriteria(query.getExpressionBuilder().get("firstName").equal("Anthony").and(query.getExpressionBuilder().get("lastName").equal("Anthony")));
            readPerson = (Employee) ((Vector) getSession().executeQuery(query)).firstElement();
        }
    };
    Thread thread = new Thread(runnable);
    thread.start();
    try {
        Thread.sleep(3000);
    } catch (InterruptedException ex) {
    }
    ((UnitOfWorkImpl) uow).mergeClonesAfterCompletion();
    try {
        thread.join();
    } catch (InterruptedException ex) {
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) Vector(java.util.Vector)

Example 8 with UnitOfWorkImpl

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

the class ExpressionBuilderTestSuite method testUnitOfWorkInExpressionBuilder.

// Bug# 429232 - Instance of UnitOfWork is stored in ExpressionBuilder inside ConcurrentFixedCache
// Make sure that [ExpressionBuilder].setSession() will not store UnitOfWork inside builder even
// when UnitOfWork is passed as an argument.
public void testUnitOfWorkInExpressionBuilder() {
    ExpressionBuilder builder = new ExpressionBuilder();
    UnitOfWorkImpl uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
    builder.setSession(uow);
    AbstractSession session = builder.getSession();
    if (session instanceof UnitOfWork) {
        throw new TestErrorException("Session stored in ExpressionBuilder shall not be UnitOfWork.");
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 9 with UnitOfWorkImpl

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

the class WriteChanges_Release_TestCase method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    uow.writeChanges();
    uow.release();
    if (((UnitOfWorkImpl) uow).isInTransaction()) {
        throw new TestErrorException("Even though release was called, since we already wrote changes and started the transaction, it must be rolled back.");
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 10 with UnitOfWorkImpl

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

the class WriteChangesFailed_TestCase method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    try {
        Employee employee = new Employee();
        employee = (Employee) uow.registerNewObject(employee);
        employee.setId(id);
        employee.setFirstName("Andrew");
        try {
            uow.writeChanges();
        } catch (Exception e) {
            exception = e;
        }
        if (exception == null) {
            return;
        }
        // added this kind of testing for the CommitTransactionPending state.
        if (((UnitOfWorkImpl) uow).getLifecycle() != UnitOfWorkImpl.WriteChangesFailed) {
            throw new TestErrorException("Lifecycle state not set to WriteChangesFailed.  Instead it was: " + ((UnitOfWorkImpl) uow).getLifecycle());
        }
        // black box test one scenario just for sanity.
        try {
            uow.revertAndResume();
            throw new TestErrorException("Exception not thrown calling revertAndResume after writeChanges failed.");
        } catch (Exception expected) {
            if (!((expected instanceof ValidationException) && (((ValidationException) expected).getErrorCode() == ValidationException.UNIT_OF_WORK_AFTER_WRITE_CHANGES_FAILED))) {
                throw new TestErrorException("Exception not thrown calling revertAndResume after writeChanges.  Instead triggered other exception.", expected);
            }
        }
        // test that transaction rolled back.
        if (getAbstractSession().isInTransaction()) {
            throw new TestErrorException("Should not be in transaction after a failed writeChanges.");
        }
    } finally {
        uow.release();
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) ValidationException(org.eclipse.persistence.exceptions.ValidationException) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) ValidationException(org.eclipse.persistence.exceptions.ValidationException)

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