@Test public void testQueryUsingLockOptions() { // todo : need an association here to make sure the alias-specific lock modes are applied correctly doInHibernate( this::sessionFactory, session -> { session.createQuery( "from A a" ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) ) .uniqueResult(); session.createQuery( "from A a" ) .setLockOptions( new LockOptions().setAliasSpecificLockMode( "a", LockMode.PESSIMISTIC_WRITE ) ) .uniqueResult(); } ); }
@Test public void testPessimisticLockWithUnionThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Vehicle> vehicles = session.createQuery( "select v from Vehicle v" ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) ) .getResultList(); assertEquals( 3, vehicles.size() ); assertEquals( 4, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Override protected void applySkipLocked(Query query) { query.setLockOptions( new LockOptions( lockMode() ).setFollowOnLocking( false ) ); }
protected void applySkipLocked(Query query) { query.setLockOptions( new LockOptions( lockMode() ) .setTimeOut( LockOptions.SKIP_LOCKED ) ); }
@Test public void testPessimisticLockWithCountDistinctThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Product> products = session.createQuery( "select p from Product p where ( select count(distinct p1.id) from Product p1 ) > 0 ", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setFollowOnLocking( false ) ) .getResultList(); assertEquals( 50, products.size() ); assertEquals( 1, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithGroupByThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Object[]> products = session.createQuery( "select count(p), p " + "from Product p " + "group by p.id, p.name " ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) ) .getResultList(); assertEquals( 50, products.size() ); assertEquals( 51, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithDistinctThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Product> products = session.createQuery( "select distinct p from Product p", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) ) .getResultList(); assertEquals( 50, products.size() ); assertEquals( 51, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithMaxResultsThenNoFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Product> products = session.createQuery( "select p from Product p", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) ) .setMaxResults( 10 ) .getResultList(); assertEquals( 10, products.size() ); assertEquals( 1, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithMaxResultsAndOrderByThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Product> products = session.createQuery( "select p from Product p order by p.id", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) ) .setMaxResults( 10 ) .getResultList(); assertEquals( 10, products.size() ); assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithGroupByWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Object[]> products = session.createQuery( "select count(p), p " + "from Product p " + "group by p.id, p.name " ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setFollowOnLocking( true ) ) .getResultList(); assertEquals( 50, products.size() ); assertEquals( 51, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithFirstResultsThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Product> products = session.createQuery( "select p from Product p", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) ) .setFirstResult( 40 ) .setMaxResults( 10 ) .getResultList(); assertEquals( 10, products.size() ); assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Product> products = session.createQuery( "select p from Product p order by p.id", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setFollowOnLocking( true ) ) .setMaxResults( 10 ) .getResultList(); assertEquals( 10, products.size() ); assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithDistinctWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Product> products = session.createQuery( "select distinct p from Product p where p.id > 40" ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setFollowOnLocking( true ) ) .setMaxResults( 10 ) .getResultList(); assertEquals( 10, products.size() ); assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test @RequiresDialect({ Oracle8iDialect.class, PostgreSQL81Dialect.class, SQLServer2005Dialect.class } ) public void testNoWait() throws NoSuchFieldException, IllegalAccessException { Session session = sessionFactory().openSession(); session.beginTransaction(); try { session.createQuery( "select a from A a", A.class ) .unwrap( org.hibernate.query.Query.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setTimeOut( LockOptions.NO_WAIT ) ) .list(); String lockingQuery = sqlStatementInterceptor.getSqlQueries().getLast(); assertTrue( lockingQuery.toLowerCase().contains( "nowait") ); } finally { session.getTransaction().commit(); session.close(); } }
@Test public void testPessimisticLockWithFirstResultsWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); List<Product> products = session.createQuery( "select p from Product p", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setFollowOnLocking( true ) ) .setFirstResult( 40 ) .setMaxResults( 10 ) .getResultList(); assertEquals( 10, products.size() ); assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() ); session.getTransaction().commit(); session.close(); }
@Test public void testPessimisticLockWithUnionWhileExplicitlyDisablingFollowOnLockingThenFails() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); try { List<Vehicle> vehicles = session.createQuery( "select v from Vehicle v" ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setFollowOnLocking( false ) ) .getResultList(); fail( "Should throw exception since Oracle does not support UNION if follow on locking is disabled" ); } catch ( PersistenceException expected ) { assertEquals( SQLGrammarException.class, expected.getCause().getClass() ); } }
@Test public void testPessimisticLockWithDistinctWhileExplicitlyDisablingFollowOnLockingThenFails() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); try { List<Product> products = session.createQuery( "select distinct p from Product p where p.id > 40", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setFollowOnLocking( false ) ) .getResultList(); fail( "Should throw exception since Oracle does not support DISTINCT if follow on locking is disabled" ); } catch ( PersistenceException expected ) { assertEquals( SQLGrammarException.class, expected.getCause().getClass() ); } }
@Test public void testPessimisticLockWithGroupByWhileExplicitlyDisablingFollowOnLockingThenFails() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); try { List<Object[]> products = session.createQuery( "select count(p), p " + "from Product p " + "group by p.id, p.name " ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setFollowOnLocking( false ) ) .getResultList(); fail( "Should throw exception since Oracle does not support GROUP BY if follow on locking is disabled" ); } catch ( PersistenceException expected ) { assertEquals( SQLGrammarException.class, expected.getCause().getClass() ); } }
@Test public void testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyDisablingFollowOnLockingThenFails() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); try { List<Product> products = session.createQuery( "select p from Product p order by p.id", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setFollowOnLocking( false ) ) .setMaxResults( 10 ) .getResultList(); fail( "Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled" ); } catch ( PersistenceException expected ) { assertEquals( SQLGrammarException.class, expected.getCause().getClass() ); } }
@Test public void testPessimisticLockWithFirstResultsWhileExplicitlyDisablingFollowOnLockingThenFails() { final Session session = openSession(); session.beginTransaction(); sqlStatementInterceptor.getSqlQueries().clear(); try { List<Product> products = session.createQuery( "select p from Product p", Product.class ) .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) .setFollowOnLocking( false ) ) .setFirstResult( 40 ) .setMaxResults( 10 ) .getResultList(); fail( "Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled" ); } catch ( PersistenceException expected ) { assertEquals( SQLGrammarException.class, expected.getCause().getClass() ); } }