I'm using some code to monitor my Tomcat (9) connection pools. However, when I scan for the Datasource MBeans, the returned results are not consistent. Does anyone have an explanation for why this might be?
Sometimes I get three MBeans (Datasource + connection pool + connection):
NAME: Catalina:type=DataSource,host=localhost,context=/,class=javax.sql.DataSource,name="jdbc/evote",connectionpool=connections,connection=1
DESC: Information on the management interface of the MBean
CLAS: org.apache.tomcat.dbcp.dbcp2.PoolableConnection
NAME: Catalina:type=DataSource,host=localhost,context=/,class=javax.sql.DataSource,name="jdbc/evote"
DESC: null
CLAS: org.apache.tomcat.util.modeler.BaseModelMBean
NAME: Catalina:type=DataSource,host=localhost,context=/,class=javax.sql.DataSource,name="jdbc/evote",connectionpool=connections
DESC: Information on the management interface of the MBean
CLAS: org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool
But at other times, the connection pool ones don't appear in the scan. I only get the simple Datasource one:
NAME: Catalina:type=DataSource,host=localhost,context=/,class=javax.sql.DataSource,name="jdbc/evote"
DESC: null
CLAS: org.apache.tomcat.util.modeler.BaseModelMBean
Why would the connection pool MBean(s) not be always available?
As you noticed in your comment, the MBean corresponding to the connection pool is created only after getConnection
is called for the first time.
However, since DBCP 2.8.0, which is shipped with Tomcat 9.0.38 and later, you have an action start()
on the BasicDataSourceMXBean
, which allows you to create the pool without creating any connections.