1919package org .finos .waltz .service ;
2020
2121
22+ import com .zaxxer .hikari .HikariDataSource ;
2223import org .jooq .DSLContext ;
2324import org .jooq .ExecuteContext ;
2425import org .jooq .conf .Settings ;
2829import org .slf4j .Logger ;
2930import org .slf4j .LoggerFactory ;
3031
32+ import javax .sql .DataSource ;
3133import java .util .concurrent .TimeUnit ;
3234
3335
@@ -38,6 +40,8 @@ public class SlowQueryListener extends DefaultExecuteListener {
3840
3941 private StopWatch stopWatch ;
4042 private long slowQueryThresholdInNanos ;
43+ private DataSource dataSource ;
44+ private boolean logConnectionPoolStatus ;
4145
4246 public class SQLPerformanceWarning
4347 extends Exception {
@@ -48,9 +52,11 @@ public SQLPerformanceWarning(String message) {
4852 }
4953
5054
51- public SlowQueryListener (int slowQueryThresholdSeconds ) {
55+ public SlowQueryListener (int slowQueryThresholdSeconds , DataSource dataSource , boolean logConnectionPoolStatus ) {
5256 LOG .info ("Initialising with {} second threshold" , slowQueryThresholdSeconds );
5357 this .slowQueryThresholdInNanos = TimeUnit .SECONDS .toNanos (slowQueryThresholdSeconds );
58+ this .dataSource = dataSource ;
59+ this .logConnectionPoolStatus = logConnectionPoolStatus ;
5460 }
5561
5662
@@ -71,6 +77,35 @@ public void executeEnd(ExecuteContext ctx) {
7177 new Settings ().withRenderFormatted (true ));
7278
7379 LOG .info (String .format ("Slow SQL executed in %d seconds" , TimeUnit .NANOSECONDS .toSeconds (split )), new SQLPerformanceWarning (context .renderInlined (ctx .query ())));
80+ LOG .info (dataSource .toString ());
81+
82+ logConnectionPoolStatus ();
83+ }
84+ }
85+
86+ private void logConnectionPoolStatus () {
87+ if (logConnectionPoolStatus ) {
88+ if (dataSource instanceof HikariDataSource ) {
89+ HikariDataSource hikariDataSource = (HikariDataSource ) dataSource ;
90+
91+ int activeConnections = hikariDataSource .getHikariPoolMXBean ().getActiveConnections ();
92+ int idleConnections = hikariDataSource .getHikariPoolMXBean ().getIdleConnections ();
93+ int totalConnections = hikariDataSource .getHikariPoolMXBean ().getTotalConnections ();
94+ int threadsAwaitingConnection = hikariDataSource .getHikariPoolMXBean ().getThreadsAwaitingConnection ();
95+ String poolName = hikariDataSource .getPoolName ();
96+
97+ LOG .info (
98+ "Hikari Pool Status [{}]: Active=[{}], Idle=[{}], Total=[{}], Awaiting=[{}]" ,
99+ poolName ,
100+ activeConnections ,
101+ idleConnections ,
102+ totalConnections ,
103+ threadsAwaitingConnection );
104+ } else if (dataSource != null ) {
105+ LOG .info ("DataSource is of type: {}" , dataSource .getClass ().getName ());
106+ } else {
107+ LOG .warn ("DataSource is null, cannot log pool status." );
108+ }
74109 }
75110 }
76111}
0 commit comments