hibernate jconsole spring配置
我正在努力配置HibernateJMX,以便在HibernateJConsole插件中获得一些指标 实际上,我遵循了hibernate jconsole插件官方网站的配置: 但它不起作用,所以我在网上搜索了几个小时,测试了一些东西。我发现唯一与我的问题相关的是: 但它仍然不起作用。我需要你的帮助 以下是配置:hibernate jconsole spring配置,spring,hibernate,jconsole,Spring,Hibernate,Jconsole,我正在努力配置HibernateJMX,以便在HibernateJConsole插件中获得一些指标 实际上,我遵循了hibernate jconsole插件官方网站的配置: 但它不起作用,所以我在网上搜索了几个小时,测试了一些东西。我发现唯一与我的问题相关的是: 但它仍然不起作用。我需要你的帮助 以下是配置: @PersistenceContext(unitName = DomainConstants.JPA_PU_BACKEND) private EntityManager em; @Bea
@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;
}
@PersistenceContext(unitName=DomainConstants.JPA\u PU\u后端)
私人实体管理者;
@Bean(name=“jmxExporter”)
公共MBeanExporter jmxExporter()引发格式错误的ObjectNameException、InstanceAlreadyExistsException、MBeanRegistrationException、NotCompliantBeanException{
MBeanExporter exporter=新的MBeanExporter();
Map bean=新的HashMap();
put(“Hibernate:application=Statistics”、“hibernatestaticsbean”);
MBeanServerFactoryBean服务器工厂=新的MBeanServerFactoryBean();
serverFactory.SetLocateExistingServerIfAbsible(true);
//---新1
MBeanServer MBeanServer=serverFactory.getObject();
exporter.setServer(MBeanServer);
exporter.setRegistrationPolicy(RegistrationPolicy.REPLACE_-EXISTING);
//结束--新建1
出口植物(豆类);
退货出口商;
}
@Bean(name=“hibernatestaticsbean”)
公共统计服务HibernatisticsBean(){
统计服务=新统计服务();
服务。setStatisticsEnabled(真);
service.setSessionFactory(((会话)em.getDelegate()).getSessionFactory());
回程服务;
}
对于hibernate配置,我还将hibernate.generate_statistics设置为true
我卡住了。我真的需要这个工具来工作,因为我们有需要很多时间的查询。这个工具将是完美的
编辑:
MBean似乎已加载。当我执行查询时,属性会更改。
但是当我试图调用其中一个操作时:getQueryStatistics、getCollectionStatistics等等。。我得到以下错误:
实际上,我没有关于查询的统计信息,没有显示任何信息:
我知道这篇文章已经两年了,但我想与大家分享一下,以防其他人在使用最新的Spring和Hibernate时遇到问题 我的环境是Java8、SpringBoot1.4.0(Spring4.3.2和Hibernate5.0.9) 我尝试了人们在Hibernate4.3中提到的几种不同的方法,但它们对我不起作用。最后,我决定听取在Hibernate特性请求论坛(要求恢复JMX对Hibernate统计数据的支持)中找到的其他人的一些建议,他们建议从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();
}
}
我的回答很好
在我的情况下,我需要稍微调整一下以使其工作:
...
@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();
}
...
尝试直接设置bean引用,而不是其名称:
beans.put(“Hibernate:application=Statistics”,hibernateStatisticsBean())
。感谢Andrei的帮助。我尝试了你的解决方案,但它没有改变任何事情。当你说它不起作用时,那是什么意思?MBean不会出现在JMX中,您可以连接到JMX服务器等?你能发布一些日志吗?我编辑了主要帖子。在启动应用程序时,我得到了以下日志:mai 042014 9:10:22 PM org.springframework.jmx.export.MBeanExporter afterPropertiesSet Infos:在启动mai 04时注册jmx暴露的bean,2014年9:10:22 PM org.springframework.jmx.export.MBeanExporter registerBean实例信息:找到MBean“Hibernate:application=Statistics”:在jmx服务器上注册为MBean[Hibernate:application=Statistics]文档说Hibernate jar需要位于jconsole的类路径上。你把它放在应该放的地方了吗?