Skip to content

Commit d7f43d5

Browse files
Merge pull request #7443 from deutschebank/db-contrib/waltz-7434-log-database-connection-pool-in-waltz
Db contrib/waltz 7434 log database connection pool in waltz
2 parents f6aa54e + 38a18b6 commit d7f43d5

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

waltz-jobs/src/main/resources/logback.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
<logger name="org.finos" level="DEBUG" />
3030
<!--<logger name="org.jooq" level="DEBUG" />-->
3131

32+
<logger name="WALTZ.PERFORMANCE" level="INFO" />
33+
3234
<root level="WARN">
3335
<appender-ref ref="STDOUT" />
3436
</root>

waltz-service/src/main/java/org/finos/waltz/service/DIBaseConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public class DIBaseConfiguration {
7474
@Value("${database.performance.query.slow.threshold:10}")
7575
private int databasePerformanceQuerySlowThreshold;
7676

77+
@Value("${log.connection.pool.status:false}")
78+
private boolean logConnectionPoolStatus;
79+
7780
@Bean
7881
public DataSource dataSource() {
7982

@@ -120,7 +123,7 @@ public DSLContext dsl(DataSource dataSource) {
120123
.set(dslSettings)
121124
.set(
122125
//new SlowDatabaseConnectionSimulator(2000),
123-
new SlowQueryListener(databasePerformanceQuerySlowThreshold),
126+
new SlowQueryListener(databasePerformanceQuerySlowThreshold, dataSource, logConnectionPoolStatus),
124127
new SpringExceptionTranslationExecuteListener(new SQLStateSQLExceptionTranslator()));
125128

126129
return DSL.using(configuration);

waltz-service/src/main/java/org/finos/waltz/service/SlowQueryListener.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.finos.waltz.service;
2020

2121

22+
import com.zaxxer.hikari.HikariDataSource;
2223
import org.jooq.DSLContext;
2324
import org.jooq.ExecuteContext;
2425
import org.jooq.conf.Settings;
@@ -28,6 +29,7 @@
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
3031

32+
import javax.sql.DataSource;
3133
import 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

Comments
 (0)