ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

休眠jconsole弹簧配置

2019-11-21 20:20:01  阅读:181  来源: 互联网

标签:jconsole hibernate spring


我正在努力配置休眠jmx,以便使用休眠jconsole插件获得一些指标.

实际上,我是从休眠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

解决方法:

我知道这篇文章已有两年历史了,但是我想分享一下这篇文章,以防其他人无法使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注释从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();
    }

}

标签:jconsole,hibernate,spring
来源: https://codeday.me/bug/20191121/2054416.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有