9

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.

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89

4 Answers4

7

If you are using spring boot:

new HikariDataSourcePoolMetadata(dataSource).getActive();
Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
  • 4
    Ah, ah. [I know, thanks!](https://github.com/spring-projects/spring-boot/blob/a79131f8d2ed5817288717014f20dafdbc3e2412/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/metadata/HikariDataSourcePoolMetadata.java#L29) – Stephane Nicoll Aug 20 '16 at 16:04
  • 1
    In my case i had to add casting - new HikariDataSourcePoolMetadata((HikariDataSource) dataSource).getMax() – Dev Fh Aug 29 '19 at 10:10
2

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

brettw
  • 10,664
  • 2
  • 42
  • 59
  • thanks for the answer Brett. I am creating and managing the `HikariDataSource` myself. If that's so "easy", can't you just give access to those information using the API? I can't and won't use JMX for this. Thanks. – Stephane Nicoll Jun 13 '14 at 15:41
  • I will consider it for an upcoming release. The thing about adding API, when you have a project that thousands of users are using, is that you better be 100% satisfied with what you choose because changing it is very difficult once it's out in the wild. What is the usecase for needing active connections in your scenario? – brettw Jun 13 '14 at 22:51
  • Check issue [#1013](https://github.com/spring-projects/spring-boot/issues/1013) of the [Spring Boot project](http://projects.spring.io/spring-boot/) – Stephane Nicoll Jun 14 '14 at 16:11
2

This can be done very directly.

dataSource.hikariPoolMXBean.activeConnections
F. P. Freely
  • 1,026
  • 14
  • 24
1

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()));
                }
            }
Ashish Sharma
  • 574
  • 7
  • 18