How to enable jmx for hibernate 4.3.11 (without spring)

As hibernate does not provide this directly, I tried following the steps given in - https://stackoverflow.com/questions/21254093/running-hibernate-4-3-with-jmx-without-spring
i.e. created a java dynamic proxy to represent the statistics interface from hibernate.
It did work to a certain extent. But it did not refresh the values. As I am using the second level cache (ehcache), I am interested in monitoring its puts, hits and misses. But they are always 0. I also printed the statistics in the logs where updated values are being printed. Can you please tell me how to achieve this?

Hibernate has support for JMX. Check out the User Guide for more details. Check out the JmxServiceImpl class as well.

Thanks!
I added the below in my HibernateUtil.java -

HashMap<String, String> configMap = new HashMap<>();
configMap.put("hibernate.jmx.usePlatformServer" , "true");
JmxService jmxService = new JmxServiceImpl(configMap);
Statistics statistics = sessionFactory.getStatistics();
jmxService.registerMBean(new ObjectName("Hibernate:application=Statistics"),statistics);

But it fails with -

Unable to register MBean [ON=Hibernate:application=Statistics]
org.hibernate.HibernateException: Unable to register MBean [ON=Hibernate:application=Statistics]
        at org.hibernate.jmx.internal.JmxServiceImpl.registerMBean(JmxServiceImpl.java:169)
        at com.avaya.pim.jdbc.hibernate.HibernateUtil.getSessionFactory(HibernateUtil.java:266)
        at com.avaya.pim.jdbc.hibernate.HibernateUtil.openSession(HibernateUtil.java:336)
        at com.avaya.pim.util.SysConfig.getConfigValueAsBoolean(SysConfig.java:408)
        at com.avaya.pim.pam.PAM.<clinit>(PAM.java:105)
Caused by: javax.management.NotCompliantMBeanException: MBean class org.hibernate.stat.internal.ConcurrentStatisticsImpl does not implement DynamicMBean, and neither follows the Standard MBean conventions (javax.management.NotCompliantMBeanException: Class org.hibernate.stat.internal.ConcurrentStatisticsImpl is not a JMX compliant Standard MBean) nor the MXBean conventions (javax.management.NotCompliantMBeanException: org.hibernate.stat.internal.ConcurrentStatisticsImpl: Class org.hibernate.stat.internal.ConcurrentStatisticsImpl is not a JMX compliant MXBean)
        at com.sun.jmx.mbeanserver.Introspector.checkCompliance(Introspector.java:176)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:317)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
        at org.hibernate.jmx.internal.JmxServiceImpl.registerMBean(JmxServiceImpl.java:162)
        ... 4 more

Why did you try to register the statistics object? Hibernate should register the MBean for you. Try to debug the JMXService and see why it does not work for you.

Do you mean to say that below lines 4 & 5 are not required? The constructor of JmxServiceImpl.java does not register the MBean. I think we need to call some method to register the MBean. Otherwise how will it know what to register, beacause we are not passing the ObjectName in JmxServiceImpl constructor.

REQUIRED
1. HashMap<String, String> configMap = new HashMap<>();
2. configMap.put("hibernate.jmx.usePlatformServer" , "true");
3. JmxService jmxService = new JmxServiceImpl(configMap);

NOT REQUIRED
4. Statistics statistics = sessionFactory.getStatistics();
5. jmxService.registerMBean(new ObjectName("Hibernate:application=Statistics"),statistics);

You don’t need to create the JmxServiceImpl object. Hibernate can do that for you.

Did you debug the JmxServiceImpl object during Hibernate bootstrap? Does it register the Services?

Can you please share an example on usage of jmxServie or point to a url? The Javadoc also does not provide much information nor any sample code is available on the net. I have still not debugged jmxServiceImpl.

Check out this StackOverflow answer for a workaround.

I noticed there’s a Jira issue that we need to fix.

This is the same link that I had provided in my initial mail. As I mentioned, it does show up Hibernate statistics in the Jconsole, but does not refresh.

Did you create the StatisticsMXBean?

Because your error message says that:

Caused by: javax.management.NotCompliantMBeanException: MBean class org.hibernate.stat.internal.ConcurrentStatisticsImpl does not implement DynamicMBean, and neither follows the Standard MBean conventions (javax.management.NotCompliantMBeanException: Class org.hibernate.stat.internal.ConcurrentStatisticsImpl is not a JMX compliant Standard MBean) nor the MXBean conventions (javax.management.NotCompliantMBeanException: org.hibernate.stat.internal.ConcurrentStatisticsImpl: Class org.hibernate.stat.internal.ConcurrentStatisticsImpl is not a JMX compliant MXBean)

Which shouldn’t be the case if you exposed the StatisticsMXBean instead.

Yes, I did.

import javax.management.MXBean;

import org.hibernate.stat.Statistics;

@MXBean
public interface StatisticsMXBean extends Statistics
{
	
}	

I started working on HHH-6190. So, you might want to watch the issue as I plan on fixing it for the 5.4 branch. Hopefully, it would be integrated to other branches once it gets fixed.

Thanks a lot. Hope it is released soon.