It's currently too entangled in our codebase (our Clock abstraction, guava)
or I'd just contribute it to jOOQ.  For now, here's the gist:

SlowQueryListener extends DefaultExecuteListener {
  private static final int MAX_QUERY_TIME_MS = 1500;

  @Override public void executeStart(ExecuteContext ctx) {
    ctx.data("start", System.currentTimeMillis());
  }

  @Override public void executeEnd(ExecuteContext ctx) {
    long duration = System.currentTimeMillis() - ctx.data("start");
    if (duration < queryThresholdInMillis) {
      return;
    }

    String[] batchSQL = ctx.batchSQL();

    if (!StringUtils.isBlank(ctx.sql())) {
      logger.warn("Slow query. %d ms spent on query: %s", duration,
formatQuery(ctx.sql()));
    } else if (batchSQL.length > 0) {
      for (int i = 0; i < batchSQL.length; i++) {
        if (batchSQL[i] != null) {
          logger.warn("Slow query. %d ms spent on batch queries. Query %d
of %d: %s",
              duration, i + 1, batchSQL.length, formatQuery(batchSQL[i]));
        } else {
          logger.warn("Slow query. %d ms spent on batch queries. Query %d
of %d missing.",
              duration, i + 1, batchSQL.length);
        }
      }
    } else {
      logger.warn("Slow query. %d ms spent on unknown query.", duration);
    }
  }

  private String formatQuery(String s) {
    s = s.replace('\n', ' ');
    s = s.replaceAll(" +", " ");
    return s.substring(0, Math.min(s.length(), MAX_QUERY_LENGTH));
  }
}



On Thu, Mar 20, 2014 at 2:34 AM, Lukas Eder <[email protected]> wrote:

> Hi Eric
>
> 2014-03-19 23:15 GMT+01:00 Eric Denman <[email protected]>:
>
>> All makes sense, thanks guys!  We're already detecting slow queries with
>> an ExecuteListener and logging the sql, seems like we just need to stick
>> with that.
>>
>
> It would be very interesting to see that in action. Would you be
> interested in sharing your ExecuteListener implementation with the
> community (e.g. under the terms of the ASL 2.0)? Other users might be able
> to profit from that.
>

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to