Dropwizard数据库运行状况检查中的SELECT查询

时间:2019-04-16 17:30:04

标签: 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(),但是它们都不起作用。如何在运行状况检查中使SELECT查询起作用?

公共类DatabaseHealthCheck扩展了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',但仍然看到相同的错误

0 个答案:

没有答案