I am using Tomcat JDBC connection pool along with Spring boot, JDBC template and SQL Server. I need to know what is going inside connection pool while application is waiting for database connection. Such as....
- No of active connections
- No of idle connections
- No of blocked connections, additional info why this connection is blocked
- No of available connections
- and ...
Is there any way to get these info by debugging or using logging frameworks like log4j?
Any idea will be appreciated.
Thanks @Sundararaj Govindasamy for great answer. Based on it, I created a component in my Spring Boot Application to debug my Database Pool information.
After a lot of research, I am able to find 3 ways to log & monitor database connection pool.
https://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html
Monitoring using Spring Boot properties.
Monitoring using JMX ( Java Management Extensions ) (as @nitin suggested)
Monitoring using Spring Aspects.
1st Way: Monitoring using Spring Boot properties.
I found below Spring boot properties which will be much useful to log & monitor database connection pool.
These properties (and some more too) were not documented. Please refer below github issue for more details. https://github.com/spring-projects/spring-boot/issues/1829
This list contains more properties which are related to datasource only.(taken from the link above)
2nd Way: Monitoring using JMX ( Java Management Extensions )
Tomcat JDBC pool provides a MBean namely ConnectionPoolMBean.
https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/tomcat/jdbc/pool/jmx/ConnectionPoolMBean.html
Spring Boot registers JMX MBeans automatically.So, no need to register/export this MBean into MBean server. Just open the JConsole which is coming with JDK, To open, In Windows-> Command prompt ->jconsole, thats it. Refer below screenshot for more info.
This MBean also notifies whenever a connection is abandoned, connection failed, when a query is taking long time etc. Refer screenshot below.
3rd Way: Monitoring using Spring Aspects (only for development/QA environment).
I use this aspect to log TomcatJdbc Connection Pool.
I created a Spring Aspect which will intercept every database call.This will surely affect the performance.
So, use this aspect in development/QA environment,comment out this method when it is not required (for example : during production deployment).
Now, you can easily identify the particular database call which creates connection leak in your application.