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());
}
}
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);
}
}
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);
}
}
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);
}
}
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);
});
}
Aggregations