1

Hello I am using dropwizard and hibernate in my application and have written the following heakthcheck. This gives me a

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

I tried changing the .getResultList() call to executeUpdate() getMaxResults(), list() but none of them work. How can I make a SELECT query work in a healthcheck?

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

I also tried making the query simpler - 'SELECT1 ' but still see the same error

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
user_mda
  • 18,148
  • 27
  • 82
  • 145
  • I copied/pasted your code with `SELECT 1` as validation query and it worked on my side. BTW don't forget to put your session in a try-with-resources statement and instead of using an entity manager you could directly call `createNativeQuery` from your `session` – Nicolas Filotto Nov 19 '19 at 17:49

0 Answers0