Search in sources :

Example 1 with PessimisticLockException

use of jakarta.persistence.PessimisticLockException in project hibernate-orm by hibernate.

the class LockExceptionTests method testLockTimeoutFind.

@Test
@TestForIssue(jiraKey = "HHH-8786")
public void testLockTimeoutFind() {
    final Item item = new Item("find");
    inTransaction(session -> session.persist(item));
    try {
        inTransaction(session -> {
            session.find(Item.class, item.getId(), LockModeType.PESSIMISTIC_WRITE);
            TransactionUtil2.inTransaction(sessionFactory(), secondSession -> {
                try {
                    secondSession.find(Item.class, item.getId(), LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap(AvailableSettings.JAKARTA_LOCK_TIMEOUT, LockOptions.NO_WAIT));
                    fail("Expecting a failure");
                } catch (LockTimeoutException | PessimisticLockException expected) {
                // expected outcome
                }
            });
        });
    } finally {
        inTransaction(session -> session.createQuery("delete Item").executeUpdate());
    }
}
Also used : Item(org.hibernate.orm.test.jpa.model.Item) LockTimeoutException(jakarta.persistence.LockTimeoutException) PessimisticLockException(jakarta.persistence.PessimisticLockException) AbstractJPATest(org.hibernate.orm.test.jpa.model.AbstractJPATest) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with PessimisticLockException

use of jakarta.persistence.PessimisticLockException in project eclipselink by eclipse-ee4j.

the class AdvancedQueryTestSuite method testQueryPESSIMISTIC_READLock.

public void testQueryPESSIMISTIC_READLock() {
    if ((JUnitTestCase.getServerSession()).getPlatform().isHANA()) {
        // feature is under development (see bug 384129), but test should be skipped for the time being
        return;
    }
    // Cannot create parallel entity managers in the server.
    if (!isOnServer() && isSelectForUpateSupported()) {
        EntityManager em = createEntityManager();
        PessimisticLockException pessimisticLockException = null;
        try {
            beginTransaction(em);
            EntityManager em2 = createEntityManager();
            try {
                beginTransaction(em2);
                // 
                List employees2 = em2.createQuery("Select employee from Employee employee").getResultList();
                Employee employee2 = (Employee) employees2.get(0);
                // Find all the departments and lock them.
                List employees = em.createQuery("Select employee from Employee employee").setLockMode(LockModeType.PESSIMISTIC_READ).getResultList();
                Employee employee = (Employee) employees.get(0);
                employee.setFirstName("New Pessimistic Employee");
                HashMap properties = new HashMap();
                properties.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT, 0);
                em2.lock(employee2, LockModeType.PESSIMISTIC_READ, properties);
                employee2.setFirstName("Invalid Lock Employee");
                commitTransaction(em2);
            } catch (jakarta.persistence.PessimisticLockException ex) {
                pessimisticLockException = ex;
            } finally {
                closeEntityManagerAndTransaction(em2);
            }
            commitTransaction(em);
        } catch (RuntimeException ex) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            throw ex;
        } finally {
            closeEntityManager(em);
        }
        assertFalse("Proper exception not thrown when Query with LockModeType.PESSIMISTIC is used.", pessimisticLockException == null);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) HashMap(java.util.HashMap) PessimisticLockException(jakarta.persistence.PessimisticLockException) List(java.util.List) ArrayList(java.util.ArrayList) PessimisticLockException(jakarta.persistence.PessimisticLockException)

Example 3 with PessimisticLockException

use of jakarta.persistence.PessimisticLockException in project eclipselink by eclipse-ee4j.

the class AdvancedQueryTestSuite method testQueryPESSIMISTIC_WRITELock.

public void testQueryPESSIMISTIC_WRITELock() {
    if ((JUnitTestCase.getServerSession()).getPlatform().isHANA()) {
        // feature is under development (see bug 384129), but test should be skipped for the time being
        return;
    }
    // Cannot create parallel entity managers in the server.
    if (!isOnServer() && isSelectForUpateSupported()) {
        EntityManager em = createEntityManager();
        Exception pessimisticLockException = null;
        try {
            beginTransaction(em);
            EntityManager em2 = createEntityManager();
            try {
                beginTransaction(em2);
                // 
                List employees2 = em2.createQuery("Select employee from Employee employee").getResultList();
                Employee employee2 = (Employee) employees2.get(0);
                // Find all the departments and lock them.
                List employees = em.createQuery("Select employee from Employee employee").setLockMode(LockModeType.PESSIMISTIC_READ).getResultList();
                Employee employee = (Employee) employees.get(0);
                employee.setFirstName("New Pessimistic Employee");
                HashMap properties = new HashMap();
                properties.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT, 0);
                em2.lock(employee2, LockModeType.PESSIMISTIC_READ, properties);
                employee2.setFirstName("Invalid Lock Employee");
                commitTransaction(em2);
            } catch (jakarta.persistence.PessimisticLockException ex) {
                pessimisticLockException = ex;
            } finally {
                closeEntityManagerAndTransaction(em2);
            }
            commitTransaction(em);
        } catch (RuntimeException ex) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            throw ex;
        } finally {
            closeEntityManager(em);
        }
        assertFalse("Proper exception not thrown when Query with LockModeType.PESSIMISTIC is used.", pessimisticLockException == null);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) HashMap(java.util.HashMap) PessimisticLockException(jakarta.persistence.PessimisticLockException) List(java.util.List) ArrayList(java.util.ArrayList) PessimisticLockException(jakarta.persistence.PessimisticLockException) RollbackException(jakarta.persistence.RollbackException) NoResultException(jakarta.persistence.NoResultException) PersistenceException(jakarta.persistence.PersistenceException) QueryException(org.eclipse.persistence.exceptions.QueryException) OptimisticLockException(jakarta.persistence.OptimisticLockException)

Example 4 with PessimisticLockException

use of jakarta.persistence.PessimisticLockException in project eclipselink by eclipse-ee4j.

the class AdvancedQueryTestSuite method testLockWithSecondaryTable.

public void testLockWithSecondaryTable() {
    if ((JUnitTestCase.getServerSession()).getPlatform().isHANA()) {
        // feature is under development (see bug 384129), but test should be skipped for the time being
        return;
    }
    // Cannot create parallel entity managers in the server.
    if (!isOnServer() && isSelectForUpateSupported()) {
        EntityManager em = createEntityManager();
        Exception pessimisticLockException = null;
        try {
            beginTransaction(em);
            EntityManager em2 = createEntityManager();
            try {
                beginTransaction(em2);
                List employees2 = em2.createQuery("Select employee from Employee employee").getResultList();
                Employee employee2 = (Employee) employees2.get(0);
                // Find all the employees and lock them.
                List employees = em.createQuery("Select employee from Employee employee").setLockMode(LockModeType.PESSIMISTIC_WRITE).getResultList();
                Employee employee = (Employee) employees.get(0);
                employee.setSalary(90000);
                HashMap properties = new HashMap();
                properties.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT, 0);
                em2.lock(employee2, LockModeType.PESSIMISTIC_WRITE, properties);
                employee2.setSalary(100000);
                commitTransaction(em2);
            } catch (PessimisticLockException ex) {
                pessimisticLockException = ex;
            } finally {
                closeEntityManagerAndTransaction(em2);
            }
            commitTransaction(em);
        } catch (RuntimeException ex) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            throw ex;
        } finally {
            closeEntityManager(em);
        }
        assertFalse("Proper exception not thrown when Query with LockModeType.PESSIMISTIC is used.", pessimisticLockException == null);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) PessimisticLockException(jakarta.persistence.PessimisticLockException) RollbackException(jakarta.persistence.RollbackException) NoResultException(jakarta.persistence.NoResultException) PersistenceException(jakarta.persistence.PersistenceException) QueryException(org.eclipse.persistence.exceptions.QueryException) OptimisticLockException(jakarta.persistence.OptimisticLockException) PessimisticLockException(jakarta.persistence.PessimisticLockException)

Example 5 with PessimisticLockException

use of jakarta.persistence.PessimisticLockException in project hibernate-orm by hibernate.

the class LockTest method testUpdateWithPessimisticReadLockWithoutNoWait.

@Test
@RequiresDialectFeature(value = DialectChecks.SupportsLockTimeouts.class)
public void testUpdateWithPessimisticReadLockWithoutNoWait() {
    Lock lock = new Lock();
    lock.setName("name");
    doInJPA(this::entityManagerFactory, entityManager -> {
        entityManager.persist(lock);
    });
    doInJPA(this::entityManagerFactory, _entityManager -> {
        _entityManager.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_READ);
        AtomicBoolean failureExpected = new AtomicBoolean();
        try {
            doInJPA(this::entityManagerFactory, entityManager -> {
                try {
                    TransactionUtil.setJdbcTimeout(entityManager.unwrap(Session.class));
                    entityManager.createNativeQuery(updateStatement()).setParameter("name", "changed").setParameter("id", lock.getId()).executeUpdate();
                } catch (Exception e) {
                    if (ExceptionUtil.isSqlLockTimeout(e)) {
                        failureExpected.set(true);
                    }
                }
            });
        } catch (Exception e) {
            if (!failureExpected.get()) {
                fail("Should throw LockTimeoutException or PessimisticLockException");
            }
        }
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Lock _lock = entityManager.merge(lock);
        entityManager.remove(_lock);
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PessimisticLockException(jakarta.persistence.PessimisticLockException) PersistenceException(jakarta.persistence.PersistenceException) QueryTimeoutException(jakarta.persistence.QueryTimeoutException) TransactionException(org.hibernate.TransactionException) ExecutionException(java.util.concurrent.ExecutionException) LockTimeoutException(jakarta.persistence.LockTimeoutException) OptimisticLockException(jakarta.persistence.OptimisticLockException) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature)

Aggregations

PessimisticLockException (jakarta.persistence.PessimisticLockException)8 LockTimeoutException (jakarta.persistence.LockTimeoutException)5 OptimisticLockException (jakarta.persistence.OptimisticLockException)4 PersistenceException (jakarta.persistence.PersistenceException)4 EntityManager (jakarta.persistence.EntityManager)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)3 AbstractJPATest (org.hibernate.orm.test.jpa.model.AbstractJPATest)3 Item (org.hibernate.orm.test.jpa.model.Item)3 Test (org.junit.jupiter.api.Test)3 NoResultException (jakarta.persistence.NoResultException)2 RollbackException (jakarta.persistence.RollbackException)2 QueryException (org.eclipse.persistence.exceptions.QueryException)2 QueryTimeoutException (jakarta.persistence.QueryTimeoutException)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Session (org.hibernate.Session)1 TransactionException (org.hibernate.TransactionException)1