From 800b60e648b5696e00d006e70f8e0fa2c296fcc0 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Thu, 11 Apr 2019 20:11:08 -0700 Subject: [PATCH] HHH-13364 : Added a test using a named query --- .../org/hibernate/jpa/test/lock/Lock.java | 9 ++++ .../org/hibernate/jpa/test/lock/LockTest.java | 44 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/Lock.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/Lock.java index 93f41f3c28..ee50e511f5 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/Lock.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/Lock.java @@ -10,12 +10,21 @@ package org.hibernate.jpa.test.lock; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.LockModeType; +import javax.persistence.NamedQuery; +import javax.persistence.QueryHint; import javax.persistence.Version; /** * @author Emmanuel Bernard */ @Entity(name="Lock_") +@NamedQuery( + name="AllLocks", + query="from Lock_", + lockMode = LockModeType.PESSIMISTIC_WRITE, + hints = { @QueryHint( name = "javax.persistence.lock.timeout", value = "0")} +) public class Lock { private Integer id; private Integer version; diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/LockTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/LockTest.java index 29902c33c9..dc6e41c282 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/LockTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/LockTest.java @@ -220,6 +220,50 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase { } ); } + @Test(timeout = 5 * 1000) //5 seconds + @TestForIssue( jiraKey = "HHH-13364" ) + @RequiresDialectFeature( value = DialectChecks.SupportsLockTimeouts.class, + comment = "Test verifies proper exception throwing when a lock timeout is specified for NamedQuery#getResultList.", + jiraKey = "HHH-13364" ) + public void testNamedQueryResultListPessimisticWriteLockTimeoutException() { + Lock lock = new Lock(); + lock.setName( "name" ); + + doInJPA( this::entityManagerFactory, entityManager -> { + entityManager.persist( lock ); + } ); + + doInJPA( this::entityManagerFactory, _entityManager -> { + + Lock lock2 = _entityManager.find( Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE ); + assertEquals( "lock mode should be PESSIMISTIC_WRITE ", LockModeType.PESSIMISTIC_WRITE, _entityManager.getLockMode( lock2 ) ); + + doInJPA( this::entityManagerFactory, entityManager -> { + try { + TransactionUtil.setJdbcTimeout( entityManager.unwrap( Session.class ) ); + entityManager.createNamedQuery( "AllLocks", Lock.class ).getResultList(); + fail( "Exception should be thrown" ); + } + catch (LockTimeoutException lte) { + // Proper exception thrown for dialect supporting lock timeouts when an immediate timeout is set. + lte.getCause(); + } + catch (PessimisticLockException pe) { + fail( "Find with immediate timeout should have thrown LockTimeoutException." ); + } + catch (PersistenceException pe) { + log.info( + "EntityManager.find() for PESSIMISTIC_WRITE with timeout of 0 threw a PersistenceException.\n" + + "This is likely a consequence of " + getDialect().getClass() + .getName() + " not properly mapping SQL errors into the correct HibernateException subtypes.\n" + + "See HHH-7251 for an example of one such situation.", pe + ); + fail( "EntityManager should be throwing LockTimeoutException." ); + } + } ); + } ); + } + @Test @RequiresDialectFeature( value = DialectChecks.SupportSkipLocked.class ) public void testUpdateWithPessimisticReadLockSkipLocked() {