-1

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;
    }

JConsole

J.Olufsen
  • 13,415
  • 44
  • 120
  • 185

1 Answers1

1

Write a new @Bean method to expose the StatisticsService

@Autowired
@Bean
public StatisticsService service(SessionFactory sessionFactory) {
    StatisticsService statsMBean = new StatisticsService();
    statsMBean.setSessionFactory(sessionFactory);
    statsMBean.setStatisticsEnabled(true);

    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    mBeanServer.registerMBean(statsMBean, new ObjectName("Hibernate:application=Statistics"));
    return statsMBean;
}

Alternatively, you can invoke afterPropertiesSet and getObject on sessionFactoryBean to get the SessionFactory instance. Note that you will have to check if getObject returns the same object on future invocations. You don't want it to return one instance for use with your MBean and another instance for the rest of your app.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Actually `sessionFactory`method has one more annotation: `@DependsOn("flyway")`. Will it interfere with `StatisticsService ` bean? What kind of type will work for `ObjectName`. I tried: `javax.mamagement.ObjectName` and `org.hibernate.metamodelrelational.ObjectName` but IDE refuses to work with those. – J.Olufsen Mar 10 '15 at 17:14
  • @RCola No, the `@DependsOn` will have no effect here. It will simply force the `flyway` bean to be prepared before both the `StatisticsService` and `LocalSessionFactoryBean` (and `SessionFactory`) beans. – Sotirios Delimanolis Mar 10 '15 at 17:15
  • For `ObjectName`, I would assume the `javax` version, but you need the appropriate libraries. – Sotirios Delimanolis Mar 10 '15 at 17:16
  • I got error `Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.jmx.StatisticsService]: Factory method 'service' threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/stat/StatisticsImpl` – J.Olufsen Mar 10 '15 at 17:24
  • @RCola [Fix your classpath](http://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java). – Sotirios Delimanolis Mar 10 '15 at 17:25