2014-05-02 318 views
3

我努力配置hibernate jmx,以便通過hibernate jconsole插件獲得一些指標。休眠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,Hibernate配置。

我被卡住了。我真的需要這個工具來工作,因爲我們的查詢花費了很多時間。這個工具將是完美的。

編輯: 的的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

+0

嘗試直接設置的bean引用,而不是它的名字:'beans.put(「休眠:應用=統計」 ,hibernateStatisticsBean())'。 –

+0

謝謝安德烈的幫助。我試過你的解決方案,但它並沒有改變任何東西。 –

+0

當你說它不起作用時,這是什麼意思? MBean不會顯示在JMX中,您可以連接到JMX服務器等?你可以發佈一些日誌嗎? –

回答

1

我知道這個帖子是兩歲,但我想在其他人已經遇到麻煩的Hibernate插件的JConsole與最新的Spring和Hibernate工作的情況下分享。

我的環境是Java的8,春季啓動1.4.0(春季4.3.2和5.0.9的Hibernate)。

我想,人們提到的Hibernate 4.3爲他們工作的幾個不同的東西,但他們並沒有爲我工作。最後,我決定從我在Hibernate功能請求論壇中找到的其他人那裏得到一些建議(要求爲Hibernate統計信息帶回JMX支持),他們建議從Hibernate 3.2.7中抓取舊的StatisticsServiceMBean。

我更新了MBean並用它來包裹從休眠5使用JMX註釋統計對象,當然,它的工作。

所以你不必鍵入這一切,那就是:

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

}