Stephane Nicoll
Stephane Nicoll

Reputation: 33121

get active connection on HikariDataSource

I am trying to figure out how many connections are currently opened and I can't seem to find an obvious way to do that with Hikari.

HikariPool exposes that information (getActiveConnections) but I don't see an easy way to access that from HikariDataSource.

Upvotes: 10

Views: 21889

Answers (4)

F. P. Freely
F. P. Freely

Reputation: 1144

This can be done very directly.

dataSource.hikariPoolMXBean.activeConnections

Upvotes: 2

Ashish Sharma
Ashish Sharma

Reputation: 672

You can use below class for better monitoring:

            import javax.sql.DataSource;
            import org.aspectj.lang.JoinPoint;
            import org.aspectj.lang.annotation.After;
            import org.aspectj.lang.annotation.Aspect;
            import org.aspectj.lang.annotation.Before;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.stereotype.Component;
            import com.zaxxer.hikari.HikariDataSource;
            import com.zaxxer.hikari.pool.HikariPool;
            import lombok.extern.slf4j.Slf4j;


            @Aspect
            @Component
            @Slf4j
            public class DataSourceAspectLogger {

                private HikariPool pool;

                @Autowired
                private HikariDataSource ds;

                @Before("execution(* com.x.common.sql.repo.*.*(..))")
                public void logBeforeConnection(JoinPoint jp) throws Throwable {
                    logDataSourceInfos("Before", jp);
                }

                @After("execution(* com.x.common.sql.repo.*.*(..)) ")
                public void logAfterConnection(JoinPoint jp) throws Throwable {
                    logDataSourceInfos("After", jp);
                }

                @Autowired
                public void getPool() {
                    try {
                        java.lang.reflect.Field field = ds.getClass().getDeclaredField("pool");
                        field.setAccessible(true);
                        this.pool = (HikariPool) field.get(ds);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }

                public void logDataSourceInfos(final String time, final JoinPoint jp) {
                    final String method = String.format("%s:%s", jp.getTarget().getClass().getName(), jp.getSignature().getName());
                    int totalConnections = pool.getTotalConnections();
                    int activeConnections = pool.getActiveConnections();
                    int freeConnections = totalConnections - activeConnections;
                    int connectionWaiting = pool.getThreadsAwaitingConnection();
                    log.info(String.format("%s %s: number of connections in use by the application (active): %d.", time, method, activeConnections));
                    log.info(String.format("%s %s: the number of established but idle connections: %d.", time, method, freeConnections));
                    log.info(String.format("%s %s: number of threads waiting for a connection: %d.", time, method, connectionWaiting));
                    log.info(String.format("%s %s: max pool size: %d.", time, method, ds.getMaximumPoolSize()));
                }
            }

Upvotes: 1

Devs love ZenUML
Devs love ZenUML

Reputation: 11892

If you are using spring boot:

new HikariDataSourcePoolMetadata(dataSource).getActive();

Upvotes: 8

brettw
brettw

Reputation: 11114

You'll have to get it via JMX programmatic access. First, enable MBean registration through the registerMbeans property or by calling setRegisterMeans(). Then consult this page for how to perform programmatic access:

https://github.com/brettwooldridge/HikariCP/wiki/JMX-Monitoring

Upvotes: 2

Related Questions