org.hibernate.query.Query.setLockOptions()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(104)

本文整理了Java中org.hibernate.query.Query.setLockOptions方法的一些代码示例,展示了Query.setLockOptions的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.setLockOptions方法的具体详情如下:
包路径:org.hibernate.query.Query
类名称:Query
方法名:setLockOptions

Query.setLockOptions介绍

[英]Set the lock options for the query. Specifically only the following are taken into consideration:

  1. LockOptions#getLockMode()
  2. LockOptions#getScope()
  3. LockOptions#getTimeOut()
    For alias-specific locking, use #setLockMode(String,LockMode).
    [中]设置查询的锁定选项。具体而言,仅考虑以下因素:
    1.锁定选项#getLockMode()
    1.锁定选项#getScope()
    1.锁定选项#getTimeOut()
    对于特定于别名的锁定,请使用#setLockMode(字符串,LockMode)。

代码示例

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

@Override
protected void applySkipLocked(Query query) {
  query.setLockOptions(
    new LockOptions( lockMode() ).setFollowOnLocking( false )
  );
}

代码示例来源:origin: hibernate/hibernate-orm

protected void applySkipLocked(Query query) {
  query.setLockOptions(
      new LockOptions( lockMode() )
          .setTimeOut( LockOptions.SKIP_LOCKED )
  );
}

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

相关文章