@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; }
//Enable statistics StatisticsService statisticsService = new StatisticsService(); statisticsService.setSessionFactory(sessionFactory); statisticsService.setStatisticsEnabled(true); // Do queries... //... ///Print out stats: System.out.println(statisticsService.getQueryExecutionCount());
StatisticsService mBean = new StatisticsService(); ObjectName objectName = new ObjectName("Hibernate:type=statistics,application=<application-name>"); try{ mBean.setSessionFactory(sessionFactory); beanServer.registerMBean(mBean, objectName);
/** * @see StatisticsServiceMBean#setSessionFactoryJNDIName(java.lang.String) */ public void setSessionFactoryJNDIName(String sfJNDIName) { this.sfJNDIName = sfJNDIName; try { Object obj = new InitialContext().lookup(sfJNDIName); if (obj instanceof Reference) { Reference ref = (Reference) obj; setSessionFactory( (SessionFactory) SessionFactoryObjectFactory.getInstance( (String) ref.get(0).getContent() ) ); } else { setSessionFactory( (SessionFactory) obj ); } } catch (NameNotFoundException e) { log.error("No session factory with JNDI name " + sfJNDIName, e); setSessionFactory(null); } catch (NamingException e) { log.error("Error while accessing session factory with JNDI name " + sfJNDIName, e); setSessionFactory(null); } catch (ClassCastException e) { log.error("JNDI name " + sfJNDIName + " does not handle a session factory reference", e); setSessionFactory(null); } }
StatisticsService mBean = new StatisticsService(); mBean.setSessionFactory(sessionFactory); server.registerMBean(mBean, objectName); } catch (InstanceAlreadyExistsException iaee) {
/** * @see StatisticsServiceMBean#setSessionFactoryJNDIName(java.lang.String) */ public void setSessionFactoryJNDIName(String sfJNDIName) { this.sfJNDIName = sfJNDIName; try { Object obj = new InitialContext().lookup(sfJNDIName); if (obj instanceof Reference) { Reference ref = (Reference) obj; setSessionFactory( (SessionFactory) SessionFactoryObjectFactory.getInstance( (String) ref.get(0).getContent() ) ); } else { setSessionFactory( (SessionFactory) obj ); } } catch (NameNotFoundException e) { log.error("No session factory with JNDI name " + sfJNDIName, e); setSessionFactory(null); } catch (NamingException e) { log.error("Error while accessing session factory with JNDI name " + sfJNDIName, e); setSessionFactory(null); } catch (ClassCastException e) { log.error("JNDI name " + sfJNDIName + " does not handle a session factory reference", e); setSessionFactory(null); } }
try { // NOTE: lookupBean is a utility method in our app we use for jndi lookups. // replace as necessary for your application. UtilMgr utilMgr = (UtilMgr)Manager.lookupBean("UtilMgrBean", UtilMgr.class); SessionFactory sf = utilMgr.getSessionFactory(); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); // NOTE: replace myAppName as necessary ObjectName on = new ObjectName("Hibernate:type=statistics,application=myAppName"); // Enable Hibernate JMX Statistics StatisticsService statsMBean = new StatisticsService(); statsMBean.setSessionFactory(sf); statsMBean.setStatisticsEnabled(true); mbs.registerMBean(statsMBean, on); CacheManager cacheMgr = utilMgr.getCacheManager(); ManagementService.registerMBeans(cacheMgr, mbs, true, true, true, true); } catch(Throwable t) { throw new RuntimeException(t); }
/** * @see StatisticsServiceMBean#setSessionFactoryJNDIName(java.lang.String) */ public void setSessionFactoryJNDIName(String sfJNDIName) { this.sfJNDIName = sfJNDIName; try { final SessionFactory sessionFactory; final Object jndiValue = new InitialContext().lookup( sfJNDIName ); if ( jndiValue instanceof Reference ) { final String uuid = (String) ( (Reference) jndiValue ).get( 0 ).getContent(); sessionFactory = SessionFactoryRegistry.INSTANCE.getSessionFactory( uuid ); } else { sessionFactory = (SessionFactory) jndiValue; } setSessionFactory( sessionFactory ); } catch (NameNotFoundException e) { LOG.noSessionFactoryWithJndiName( sfJNDIName, e ); setSessionFactory(null); } catch (NamingException e) { LOG.unableToAccessSessionFactory( sfJNDIName, e ); setSessionFactory(null); } catch (ClassCastException e) { LOG.jndiNameDoesNotHandleSessionFactoryReference( sfJNDIName, e ); setSessionFactory(null); } }
@PostConstruct public void postConstruct() throws Exception { if (jmxEnabled) { log.debug("Adding session factory stats to JMX"); ObjectName on = new ObjectName("org.hibernate:type=Statistics"); StatisticsService stats = new StatisticsService(); stats.setSessionFactory(sessionFactory); stats.setStatisticsEnabled(true); exporter.registerManagedResource(stats, on); } else { log.debug("JMX disabled; Hibernate session factory statistics unavailable via JMX"); } }
/** * @see StatisticsServiceMBean#setSessionFactoryJNDIName(java.lang.String) */ public void setSessionFactoryJNDIName(String sfJNDIName) { this.sfJNDIName = sfJNDIName; try { final SessionFactory sessionFactory; final Object jndiValue = new InitialContext().lookup( sfJNDIName ); if ( jndiValue instanceof Reference ) { final String uuid = (String) ( (Reference) jndiValue ).get( 0 ).getContent(); sessionFactory = SessionFactoryRegistry.INSTANCE.getSessionFactory( uuid ); } else { sessionFactory = (SessionFactory) jndiValue; } setSessionFactory( sessionFactory ); } catch (NameNotFoundException e) { LOG.noSessionFactoryWithJndiName( sfJNDIName, e ); setSessionFactory(null); } catch (NamingException e) { LOG.unableToAccessSessionFactory( sfJNDIName, e ); setSessionFactory(null); } catch (ClassCastException e) { LOG.jndiNameDoesNotHandleSessionFactoryReference( sfJNDIName, e ); setSessionFactory(null); } }
private void export(String unitName, EntityManagerFactory factory) throws JpaResolutionException { if (managementService == null) { // management not enabled return; } StatisticsService statistics = new StatisticsService(); // TODO make configurable if (!(factory instanceof HibernateEntityManagerFactory)) { throw new AssertionError("Expected " + HibernateEntityManagerFactory.class.getName() + " but was " + factory.getClass().getName()); } statistics.setSessionFactory(((HibernateEntityManagerFactory) factory).getSessionFactory()); statistics.setStatisticsEnabled(true); try { managementService.export(encodeName(unitName), "Hibernate", "Hibernate session factory MBeans", statistics); } catch (ManagementException e) { throw new JpaResolutionException("Error exporting management bean for persistence unit: " + unitName, e); } }