【问题标题】:hibernate jconsole spring configurationhibernate jconsole spring 配置
【发布时间】:2014-05-02 09:34:31
【问题描述】:

我正在努力配置 hibernate jmx,以便通过 hibernate jconsole 插件获得一些指标。

其实我是按照hibernate jconsole插件官网的配置:http://hibernate-jcons.sourceforge.net/usage.html#pre-requisites

但它不起作用,所以我在互联网上搜索了几个小时,测试了一些东西。我发现的唯一与我的问题相关的事情是:How to configure Hibernate statistics in Spring 3.0 application?

但它仍然不起作用。我需要你的帮助。

这里是配置:

@PersistenceContext(unitName = DomainConstants.JPA_PU_BACKEND)
private EntityManager em;

@Bean(name="jmxExporter")
public MBeanExporter    jmxExporter() throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
    MBeanExporter exporter = new MBeanExporter();
    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put("Hibernate:application=Statistics", "hibernateStatisticsBean");
    MBeanServerFactoryBean serverFactory = new MBeanServerFactoryBean();
    serverFactory.setLocateExistingServerIfPossible(true);
    // --- new1
    MBeanServer MBeanServer = serverFactory.getObject();
    exporter.setServer(MBeanServer);
    exporter.setRegistrationPolicy(RegistrationPolicy.REPLACE_EXISTING);
    // end -- new1
    exporter.setBeans(beans);
    return exporter;
}


@Bean(name="hibernateStatisticsBean")
public StatisticsService hibernateStatisticsBean() {
    StatisticsService service = new StatisticsService();
    service.setStatisticsEnabled(true);
    service.setSessionFactory(((Session)em.getDelegate()).getSessionFactory());
    return service;
}

我还将 hibernate.generate_statistics 设置为 true 以进行休眠配置。

我被困住了。我真的需要这个工具来工作,因为我们有需要花费大量时间的查询。这个工具会很完美。

编辑: MBean 似乎已加载。当我进行查询时,属性会发生变化。 image2 http://imageshack.com/a/img838/5904/dj8c.png

但是当我尝试调用其中一项操作时:getQueryStatistics、getCollectionStatistics 等。我收到以下错误: image1 http://imageshack.com/a/img838/9693/ibkd.png

实际上我没有关于查询的统计信息,没有显示任何内容: image3 http://imageshack.com/a/img835/8088/laoz.png

【问题讨论】:

  • 尝试直接设置 bean 引用,而不是其名称:beans.put("Hibernate:application=Statistics", hibernateStatisticsBean())
  • 感谢安德烈的帮助。我尝试了您的解决方案,但没有任何改变。
  • 当你说它不起作用时,这是什么意思? MBean 不显示在 JMX 中,您可以连接到 JMX 服务器等吗?可以发一些日志吗?
  • 我编辑了主帖。我在启动应用程序时收到以下日志:mai 04, 2014 9:10:22 PM org.springframework.jmx.export.MBeanExporter afterPropertiesSet Infos: Registering beans for JMX exposure on startup mai 04, 2014 9:10:22 PM org .springframework.jmx.export.MBeanExporter registerBeanInstance 信息:位于 MBean 'Hibernate:application=Statistics':向 JMX 服务器注册为 MBean [Hibernate:application=Statistics]
  • 文档说休眠 jar 需要在 jconsole 的类路径中。你有它应该的地方吗?

标签: spring hibernate jconsole


【解决方案1】:

我知道这篇文章已经有两年了,但我想分享一下,以防其他人在让 Hibernate JConsole 插件与最新的 Spring 和 Hibernate 一起工作时遇到问题。

我的环境是 Java 8、Spring Boot 1.4.0(Spring 4.3.2 和 Hibernate 5.0.9)。

我尝试了一些人们提到的在 Hibernate 4.3 中对他们有用的不同方法,但它们对我不起作用。最后,我决定听取我在 Hibernate 功能请求论坛(要求恢复对 Hibernate Statistics 的 JMX 支持)中找到的其他人的建议,他们建议从 Hibernate 3.2.7 中获取旧的 StatisticsServiceMBean。

我更新了 MBean,并使用它使用 JMX Annotations 包装来自 Hibernate 5 的 Statistics 对象,当然它起作用了。

所以你不必输入所有这些,这里是:

import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletContext;

import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.hibernate.SessionFactory;
import org.hibernate.stat.CollectionStatistics;
import org.hibernate.stat.EntityStatistics;
import org.hibernate.stat.QueryStatistics;
import org.hibernate.stat.SecondLevelCacheStatistics;
import org.hibernate.stat.Statistics;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@Component
@ManagedResource("Hibernate:application=Statistics")
public class HibernateStatisticsMBean implements InitializingBean {

    @Autowired 
    private ServletContext servletContext;

    private Statistics stats;

    @Override
    public void afterPropertiesSet() throws Exception {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        EntityManagerFactory emf = (EntityManagerFactory) wac.getBean("entityManagerFactory");
        SessionFactory sessionFactory = ((HibernateEntityManagerFactory) emf).getSessionFactory();
        sessionFactory.getStatistics().setStatisticsEnabled(true);
        this.stats = sessionFactory.getStatistics();
    }

    @ManagedOperation
    public void clear() {
        stats.clear();
    }

    @ManagedOperation
    public EntityStatistics getEntityStatistics(String entityName) {
        return stats.getEntityStatistics(entityName);
    }

    @ManagedOperation
    public CollectionStatistics getCollectionStatistics(String role) {
        return stats.getCollectionStatistics(role);
    }

    @ManagedOperation
    public SecondLevelCacheStatistics getSecondLevelCacheStatistics(String regionName) {
        return stats.getSecondLevelCacheStatistics(regionName);
    }

    @ManagedOperation
    public QueryStatistics getQueryStatistics(String hql) {
        return stats.getQueryStatistics(hql);
    }

    @ManagedAttribute
    public long getEntityDeleteCount() {
        return stats.getEntityDeleteCount();
    }

    @ManagedAttribute
    public long getEntityInsertCount() {
        return stats.getEntityInsertCount();
    }

    @ManagedAttribute
    public long getEntityLoadCount() {
        return stats.getEntityLoadCount();
    }

    @ManagedAttribute
    public long getEntityFetchCount() {
        return stats.getEntityFetchCount();
    }

    @ManagedAttribute
    public long getEntityUpdateCount() {
        return stats.getEntityUpdateCount();
    }

    @ManagedAttribute
    public long getQueryExecutionCount() {
        return stats.getQueryExecutionCount();
    }

    @ManagedAttribute
    public long getQueryCacheHitCount() {
        return stats.getQueryCacheHitCount();
    }

    @ManagedAttribute
    public long getQueryExecutionMaxTime() {
        return stats.getQueryExecutionMaxTime();
    }

    @ManagedAttribute
    public long getQueryCacheMissCount() {
        return stats.getQueryCacheMissCount();
    }

    @ManagedAttribute
    public long getQueryCachePutCount() {
        return stats.getQueryCachePutCount();
    }

    @ManagedAttribute
    public long getFlushCount() {
        return stats.getFlushCount();
    }

    @ManagedAttribute
    public long getConnectCount() {
        return stats.getConnectCount();
    }

    @ManagedAttribute
    public long getSecondLevelCacheHitCount() {
        return stats.getSecondLevelCacheHitCount();
    }

    @ManagedAttribute
    public long getSecondLevelCacheMissCount() {
        return stats.getSecondLevelCacheMissCount();
    }

    @ManagedAttribute
    public long getSecondLevelCachePutCount() {
        return stats.getSecondLevelCachePutCount();
    }

    @ManagedAttribute
    public long getSessionCloseCount() {
        return stats.getSessionCloseCount();
    }

    @ManagedAttribute
    public long getSessionOpenCount() {
        return stats.getSessionOpenCount();
    }

    @ManagedAttribute
    public long getCollectionLoadCount() {
        return stats.getCollectionLoadCount();
    }

    @ManagedAttribute
    public long getCollectionFetchCount() {
        return stats.getCollectionFetchCount();
    }

    @ManagedAttribute
    public long getCollectionUpdateCount() {
        return stats.getCollectionUpdateCount();
    }

    @ManagedAttribute
    public long getCollectionRemoveCount() {
        return stats.getCollectionRemoveCount();
    }

    @ManagedAttribute
    public long getCollectionRecreateCount() {
        return stats.getCollectionRecreateCount();
    }

    @ManagedAttribute
    public long getStartTime() {
        return stats.getStartTime();
    }

    @ManagedAttribute
    public boolean isStatisticsEnabled() {
        return stats.isStatisticsEnabled();
    }

    @ManagedOperation
    public void setStatisticsEnabled(boolean enable) {
        stats.setStatisticsEnabled(enable);
    }

    @ManagedOperation
    public void logSummary() {
        stats.logSummary();
    }

    @ManagedAttribute
    public String[] getCollectionRoleNames() {
        return stats.getCollectionRoleNames();
    }

    @ManagedAttribute
    public String[] getEntityNames() {
        return stats.getEntityNames();
    }

    @ManagedAttribute
    public String[] getQueries() {
        return stats.getQueries();
    }

    @ManagedAttribute
    public String[] getSecondLevelCacheRegionNames() {
        return stats.getSecondLevelCacheRegionNames();
    }

    @ManagedAttribute
    public long getSuccessfulTransactionCount() {
        return stats.getSuccessfulTransactionCount();
    }

    @ManagedAttribute
    public long getTransactionCount() {
        return stats.getTransactionCount();
    }

    @ManagedAttribute
    public long getCloseStatementCount() {
        return stats.getCloseStatementCount();
    }

    @ManagedAttribute
    public long getPrepareStatementCount() {
        return stats.getPrepareStatementCount();
    }

    @ManagedAttribute
    public long getOptimisticFailureCount() {
        return stats.getOptimisticFailureCount();
    }

    @ManagedAttribute
    public String getQueryExecutionMaxTimeQueryString() {
        return stats.getQueryExecutionMaxTimeQueryString();
    }

}

【讨论】:

    【解决方案2】:

    @Chris Jensen 的精彩回答

    在我的情况下,我需要对其进行一些调整以使其正常工作:

    ...
    @Component
    @ManagedResource("Hibernate:application=Statistics")
    public class HibernateStatisticsMBean implements InitializingBean {
    
    @Autowired
    private EntityManagerFactory entityManagerFactory;
    
    private Statistics stats;
    
    @Override
    public void afterPropertiesSet() throws Exception {
        SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
        sessionFactory.getStatistics().setStatisticsEnabled(true);
        this.stats = sessionFactory.getStatistics();
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 2015-06-25
      相关资源
      最近更新 更多