Search in sources :

Example 6 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();
    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) {
            OptimisticLockException oe = (OptimisticLockException) persistenceException;
            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