コード例 #1
0
  /**
   * 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();
    }
  }