首页 > 解决方案 > Dropwizard 数据库运行状况检查中的 SELECT 查询

问题描述

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

org.hibernate.engine.jdbc.spi.SqlExceptionHelper: ResultSet is from UPDATE. No Data.  error

我尝试将.getResultList()呼叫更改为executeUpdate() getMaxResults()list()但它们都不起作用。如何使 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' 但仍然看到相同的错误

标签: databasehibernatedropwizardhibernate-native-query

解决方案


推荐阅读