Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 选择Dropwizard数据库健康检查中的查询_Database_Hibernate_Dropwizard_Hibernate Native Query - Fatal编程技术网

Database 选择Dropwizard数据库健康检查中的查询

Database 选择Dropwizard数据库健康检查中的查询,database,hibernate,dropwizard,hibernate-native-query,Database,Hibernate,Dropwizard,Hibernate Native Query,您好,我正在应用程序中使用dropwizard和hibernate,并编写了以下heakthcheck。这给了我一个机会 org.hibernate.engine.jdbc.spi.SqlExceptionHelper: ResultSet is from UPDATE. No Data. error 我尝试将.getResultList()调用更改为executeUpdate()getMaxResults(),list(),但都不起作用。如何在healthcheck中使用SELECT查询

您好,我正在应用程序中使用dropwizard和hibernate,并编写了以下heakthcheck。这给了我一个机会

org.hibernate.engine.jdbc.spi.SqlExceptionHelper: ResultSet is from UPDATE. No Data.  error
我尝试将
.getResultList()
调用更改为
executeUpdate()
getMaxResults()
list()
,但都不起作用。如何在healthcheck中使用SELECT查询

public class DatabaseHealthCheck extends HealthCheck {

  SessionFactory sessionFactory;
  private static final String validationQuery =
      "SELECT table_name FROM information_schema.tables WHERE table_schema = 'mySchema'";

  public DatabaseHealthCheck(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }


  @Override
  protected Result check() throws Exception {
    Session session = sessionFactory.openSession();
    final Transaction txn = session.beginTransaction();
    try {

      EntityManager em = session.getEntityManagerFactory().createEntityManager();
      em.createNativeQuery(validationQuery).getResultList();
      txn.commit();
    } catch (Exception e) {
      txn.rollback();
      return Result.unhealthy("Cannot execute query due to " + e.getMessage());
    } finally {
      session.close();
    }
    return Result.healthy();
  }
}

我还尝试使查询更简单—“SELECT1”,但仍然看到相同的错误

我用
SELECT1
复制/粘贴了您的代码作为验证查询,它在我这边起了作用。顺便说一句,不要忘记将您的会话放在try-with-resources语句中,您可以直接从
会话调用
createNativeQuery
,而不是使用实体管理器,我复制/粘贴了您的代码,并将
SELECT 1
作为验证查询,它在我这边起作用。顺便说一句,不要忘记将会话放在try-with-resources语句中,您可以从
会话中直接调用
createNativeQuery
,而不是使用实体管理器