/** Executes run for a minimum of number of execution or execution duration */
 @Override
 public void run() {
   logger.info(
       "\n\nThread Starting "
           + threadName
           + " ; "
           + query.getStatement()
           + " for "
           + numberOfExecutions
           + "times\n\n");
   Long start = System.currentTimeMillis();
   for (long i = numberOfExecutions;
       (i > 0 && ((System.currentTimeMillis() - start) < executionDurationInMs));
       i--) {
     try {
       synchronized (resultManager) {
         timedQuery();
         if ((System.currentTimeMillis() - lastResultWritten) > 1000) {
           resultManager.write(dataModelResult);
           lastResultWritten = System.currentTimeMillis();
         }
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   logger.info("\n\nThread exiting." + threadName + "\n\n");
 }
  /**
   * Timed query execution
   *
   * @throws Exception
   */
  private void timedQuery() throws Exception {
    boolean isSelectCountStatement =
        query.getStatement().toUpperCase().trim().contains("COUNT(*)") ? true : false;

    Connection conn = null;
    PreparedStatement statement = null;
    ResultSet rs = null;
    Long start = System.currentTimeMillis();
    Date startDate = Calendar.getInstance().getTime();
    String exception = null;
    long resultRowCount = 0;

    try {
      conn = pUtil.getConnection(query.getTenantId());
      statement = conn.prepareStatement(query.getStatement());
      boolean isQuery = statement.execute();
      if (isQuery) {
        rs = statement.getResultSet();
        while (rs.next()) {
          if (null != query.getExpectedAggregateRowCount()) {
            if (rs.getLong(1) != query.getExpectedAggregateRowCount())
              throw new RuntimeException(
                  "Aggregate count "
                      + rs.getLong(1)
                      + " does not match expected "
                      + query.getExpectedAggregateRowCount());
          }

          if (isSelectCountStatement) {
            resultRowCount = rs.getLong(1);
          } else {
            resultRowCount++;
          }
        }
      } else {
        conn.commit();
      }
    } catch (Exception e) {
      e.printStackTrace();
      exception = e.getMessage();
    } finally {
      getThreadTime()
          .getRunTimesInMs()
          .add(
              new RunTime(
                  exception,
                  startDate,
                  resultRowCount,
                  (int) (System.currentTimeMillis() - start)));

      if (rs != null) rs.close();
      if (statement != null) statement.close();
      if (conn != null) conn.close();
    }
  }