From ab71ced8c3b33adc4272411f27b775d8ec02f01a Mon Sep 17 00:00:00 2001 From: Paul Pearcy Date: Wed, 31 Oct 2018 00:49:45 -0400 Subject: [PATCH] Expose the stacktrace created when connection is aquired during leak detection. This is useful for external leak detection logging --- .../com/zaxxer/hikari/pool/ProxyConnection.java | 11 +++++++++++ .../com/zaxxer/hikari/pool/ProxyLeakTask.java | 16 ++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java b/src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java index c240f2c8c..f6951401f 100644 --- a/src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java +++ b/src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java @@ -184,6 +184,17 @@ void cancelLeakTask() leakTask.cancel(); } + /** + * Returns the stacktrace location of where the connection was acquired. If leak detection is not endabled, will + * return an empty array. + * + * @return Array containing StackTraceElement generated when connection was acquired + */ + public StackTraceElement[] getStackTrace() + { + return leakTask.getStackTrace(); + } + private synchronized T trackStatement(final T statement) { openStatements.add(statement); diff --git a/src/main/java/com/zaxxer/hikari/pool/ProxyLeakTask.java b/src/main/java/com/zaxxer/hikari/pool/ProxyLeakTask.java index 1999b71e7..65789a485 100644 --- a/src/main/java/com/zaxxer/hikari/pool/ProxyLeakTask.java +++ b/src/main/java/com/zaxxer/hikari/pool/ProxyLeakTask.java @@ -37,8 +37,9 @@ class ProxyLeakTask implements Runnable private ScheduledFuture scheduledFuture; private String connectionName; private Exception exception; - private String threadName; + private String threadName; private boolean isLeaked; + private static final StackTraceElement[] NO_STACKTRACE = {}; static { @@ -51,6 +52,12 @@ public void run() {} @Override public void cancel() {} + + @Override + public StackTraceElement[] getStackTrace() + { + return NO_STACKTRACE; + } }; } @@ -70,13 +77,18 @@ void schedule(ScheduledExecutorService executorService, long leakDetectionThresh scheduledFuture = executorService.schedule(this, leakDetectionThreshold, TimeUnit.MILLISECONDS); } + public StackTraceElement[] getStackTrace() + { + return exception.getStackTrace(); + } + /** {@inheritDoc} */ @Override public void run() { isLeaked = true; - final StackTraceElement[] stackTrace = exception.getStackTrace(); + final StackTraceElement[] stackTrace = exception.getStackTrace(); final StackTraceElement[] trace = new StackTraceElement[stackTrace.length - 5]; System.arraycopy(stackTrace, 5, trace, 0, trace.length);