Search in sources :

Example 1 with OptimisticLockException

use of org.eclipse.persistence.exceptions.OptimisticLockException in project eclipselink by eclipse-ee4j.

the class UnregisteredNewObjectOptimisticLockTest method test.

@Override
protected void test() {
    try {
        UnitOfWork uow = getSession().acquireUnitOfWork();
        Employee orig = (Employee) uow.readObject(Employee.class);
        uow.setShouldNewObjectsBeCached(true);
        Employee newEmp = (Employee) new EmployeePopulator().basicEmployeeExample6();
        orig.getManagedEmployees().add(newEmp);
        newEmp.setManager(orig);
        uow.commit();
        uow = getSession().acquireUnitOfWork();
        newEmp = (Employee) uow.readObject(newEmp);
        newEmp.setFirstName("Changed Name");
        uow.commit();
    } catch (OptimisticLockException e) {
        throw new TestErrorException("bug 3431586 unregistered new object version number not merged into cache");
    }
}
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) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException)

Example 2 with OptimisticLockException

use of org.eclipse.persistence.exceptions.OptimisticLockException in project eclipselink by eclipse-ee4j.

the class OptimisticConcurrencyJUnitTestSuite method testVersionUpdateWithIncorrectValue.

/**
 * test: updating the version field with value != in-memory value.
 * This should throw an OptimisticLockException
 */
public void testVersionUpdateWithIncorrectValue() {
    EntityManager em = createEntityManager();
    Employee employee;
    try {
        beginTransaction(em);
        employee = ModelExamples.employeeExample1();
        em.persist(employee);
        commitTransaction(em);
        beginTransaction(em);
        Employee employee1 = em.find(Employee.class, employee.getId());
        employee1.setVersion(2);
        commitTransaction(em);
        fail("updating object version with wrong value didn't throw exception");
    } catch (PersistenceException pe) {
    // expected behavior
    } catch (Exception exception) {
        Throwable persistenceException = exception;
        // Remove an wrapping exceptions such as rollback, runtime, etc.
        while (persistenceException != null && !(persistenceException instanceof OptimisticLockException)) {
            // In the server this is always a rollback exception, need to get nested exception.
            persistenceException = persistenceException.getCause();
        }
        if (persistenceException instanceof OptimisticLockException) {
            OptimisticLockException oe = (OptimisticLockException) persistenceException;
            return;
        } else {
            fail("updating object version with wrong value threw a wrong exception: " + exception.getMessage());
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) PersistenceException(jakarta.persistence.PersistenceException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException) PersistenceException(jakarta.persistence.PersistenceException)

Example 3 with OptimisticLockException

use of org.eclipse.persistence.exceptions.OptimisticLockException in project eclipselink by eclipse-ee4j.

the class OptimisticConcurrencyJUnitTestSuite method testVersionUpdateWithIncorrectValue.

/**
 * test: updating the version field with value != in-memory value.
 * This should throw an OptimisticLockException
 */
public void testVersionUpdateWithIncorrectValue() {
    EntityManager em = createEntityManager("fieldaccess");
    Employee employee;
    try {
        beginTransaction(em);
        employee = ModelExamples.employeeExample1();
        em.persist(employee);
        commitTransaction(em);
        beginTransaction(em);
        Employee employee1 = em.find(Employee.class, employee.getId());
        employee1.setVersion(2);
        commitTransaction(em);
        fail("updating object version with wrong value didn't throw exception");
    } catch (PersistenceException pe) {
    // expected behavior
    } catch (Exception exception) {
        Throwable persistenceException = exception;
        // Remove an wrapping exceptions such as rollback, runtime, etc.
        while (persistenceException != null && !(persistenceException instanceof OptimisticLockException)) {
            // In the server this is always a rollback exception, need to get nested exception.
            persistenceException = persistenceException.getCause();
        }
        if (persistenceException instanceof OptimisticLockException) {
            return;
        } else {
            fail("updating object version with wrong value threw a wrong exception: " + exception.getMessage());
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) PersistenceException(jakarta.persistence.PersistenceException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException) PersistenceException(jakarta.persistence.PersistenceException)

Example 4 with OptimisticLockException

use of org.eclipse.persistence.exceptions.OptimisticLockException in project eclipselink by eclipse-ee4j.

the class LockFailureUnitOfWorkTest method test.

@Override
protected void test() {
    UnitOfWork firstUOW = getSession().acquireUnitOfWork();
    // Read some object from the database.
    Employee employee = (Employee) firstUOW.readAllObjects(Employee.class).firstElement();
    String lastName = employee.getLastName();
    // Change the object
    boolean isMale = employee.getGender().equals("Male");
    if (isMale) {
        employee.setFemale();
    } else {
        employee.setMale();
    }
    // Update the version field explicitily
    firstUOW.executeNonSelectingCall(new org.eclipse.persistence.queries.SQLCall("UPDATE EMPLOYEE SET VERSION = VERSION + 66 WHERE L_NAME = " + "'" + lastName + "'"));
    // commit the unit of work
    try {
        // For the same uow to be reused, only use commitAndResumeOnFailure if any exception is expected.
        // So that UnitOfWorkChangeSet can be reset properly.
        firstUOW.commitAndResumeOnFailure();
    } catch (OptimisticLockException exception) {
        firstUOW.refreshObject(employee);
        // Check that refresh works.
        if (isMale != employee.getGender().equals("Male")) {
            throw new TestErrorException("Refresh does not work in unit of work.");
        }
        if (isMale) {
            employee.setFemale();
        } else {
            employee.setMale();
        }
        firstUOW.commit();
    }
    UnitOfWork secondUOW = getSession().acquireUnitOfWork();
    Employee sameEmployee = (Employee) secondUOW.readObject(employee);
    if (sameEmployee.getGender().equals("Male")) {
        sameEmployee.setFemale();
    } else {
        sameEmployee.setMale();
    }
    secondUOW.commit();
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException)

Example 5 with OptimisticLockException

use of org.eclipse.persistence.exceptions.OptimisticLockException in project eclipselink by eclipse-ee4j.

the class OptimisticConcurrencyJUnitTestSuite method testVersionUpdateWithNullValue.

/**
 * test: updating the version field with null value.
 * This should throw an exception
 */
public void testVersionUpdateWithNullValue() {
    EntityManager em = createEntityManager("fieldaccess");
    Employee employee;
    try {
        beginTransaction(em);
        employee = ModelExamples.employeeExample1();
        em.persist(employee);
        commitTransaction(em);
        beginTransaction(em);
        Employee employee2 = em.find(Employee.class, employee.getId());
        employee2.setVersion(null);
        commitTransaction(em);
        fail("employee2.setVersion(null) didn't throw exception");
    } catch (PersistenceException pe) {
    // expected behavior
    } catch (Exception exception) {
        Throwable persistenceException = exception;
        // Remove an wrapping exceptions such as rollback, runtime, etc.
        while (persistenceException != null && !(persistenceException instanceof OptimisticLockException)) {
            // In the server this is always a rollback exception, need to get nested exception.
            persistenceException = persistenceException.getCause();
        }
        if (persistenceException instanceof OptimisticLockException) {
            return;
        } else {
            fail("employee2.setVersion(null) threw a wrong exception: " + exception.getMessage());
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) PersistenceException(jakarta.persistence.PersistenceException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException) PersistenceException(jakarta.persistence.PersistenceException)

Aggregations

OptimisticLockException (org.eclipse.persistence.exceptions.OptimisticLockException)6 EntityManager (jakarta.persistence.EntityManager)4 PersistenceException (jakarta.persistence.PersistenceException)4 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)2 TestErrorException (org.eclipse.persistence.testing.framework.TestErrorException)2 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)2 EmployeePopulator (org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator)1