There is java configuration bean with Hibernate JMX Statistics Service and LocalSessionFactoryBean
configurations. I don't see any possibility to get SessionFactory through already instantiated LocalSessionFactoryBean
. The goal is to enable JMX support so JConsole would be able to access Hibernates statistics. If I create new SessionFactory it will be duplicate. How to proceed with this configuration?
@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
sessionFactoryBean.setHibernateProperties(hibProperties());
// JMX statistics
SessionFactory sf = ...; // ???
StatisticsService statsMBean = new StatisticsService();
statsMBean.setSessionFactory(sessionFactoryBean.);
statsMBean.setStatisticsEnabled(true);
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
mBeanServer.registerMBean(statsMBean, new ObjectName("Hibernate:application=Statistics"));
return sessionFactoryBean;
}