Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JMX运行Hibernate 4.3(不带spring)_Java_Hibernate_Jmx - Fatal编程技术网

Java 使用JMX运行Hibernate 4.3(不带spring)

Java 使用JMX运行Hibernate 4.3(不带spring),java,hibernate,jmx,Java,Hibernate,Jmx,我正在使用Hibernate4.3和Guice1.0运行一个JavaWebApp(wicket 6.13) 我正在尝试配置Hibernate,以便可以通过JMX框架访问运行时信息 我没有使用spring,有人能指出我是如何在Hibernate4.3上手动启用JMX的吗 我已经浏览了JmxService、JmxServiceInitiator、JmxServiceImpl、StandardServiceRegistryImpl 我还在可用设置中找到了以下设置: "hibernate.jmx.ena

我正在使用Hibernate4.3和Guice1.0运行一个JavaWebApp(wicket 6.13)

我正在尝试配置Hibernate,以便可以通过JMX框架访问运行时信息

我没有使用spring,有人能指出我是如何在Hibernate4.3上手动启用JMX的吗

我已经浏览了JmxService、JmxServiceInitiator、JmxServiceImpl、StandardServiceRegistryImpl

我还在可用设置中找到了以下设置:

"hibernate.jmx.enabled";
"hibernate.jmx.usePlatformServer";
"hibernate.jmx.agentId";
"hibernate.jmx.defaultDomain";
"hibernate.jmx.sessionFactoryName";
我已经在hibernate.cfg.xml文件中添加了jmx.enabled true设置,但没有效果

我还是不知道该怎么办


在hibernate中重构后,任何备受赞赏的帮助似乎都是一个bug。 有关详细信息,请参阅

这里是我使用的一个变通方法,使用java动态代理来表示hibernate的统计接口和默认的平台mbean服务器:

@MXBean
public interface StatisticsMXBean extends Statistics {
}

public void initStatistics(SessionFactory sessionFactory) {
    ObjectName statsName = new ObjectName("org.hibernate:type=statistics");
    MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();

    final Statistics statistics = sessionFactory.getStatistics();
    statistics.setStatisticsEnabled(true);
    Object statisticsMBean = Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { StatisticsMXBean.class }, new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(statistics, args);
            }
        });

    mbeanServer.registerMBean(statisticsMBean, statsName);
}
@MXBean
公共接口统计MXBean扩展了统计{
}
公共统计信息(SessionFactory SessionFactory){
ObjectName statsName=newobjectname(“org.hibernate:type=statistics”);
MBeanServer MBeanServer=ManagementFactory.getPlatformMBeanServer();
final Statistics=sessionFactory.getStatistics();
统计。setStatisticsEnabled(真);
Object statisticsMBean=Proxy.newProxyInstance(getClass().getClassLoader(),新类[]{StatisticsMXBean.Class},新调用处理程序(){
@凌驾
公共对象调用(对象代理、方法、对象[]args)抛出Throwable{
return method.invoke(statistics,args);
}
});
registerMBean(statisticsMBean,statsName);
}

@Harek的答案是可行的,但由于它创建了一个MXBean(更严格的MBean版本),它破坏了与Hibernate JConsole插件()的兼容性


尽管如此,这是迄今为止我见过的最好的解决方案,但大多数人可能并不知道或使用该插件。

我刚刚执行了hibernate升级

我找到的最佳解决方案是从旧的hibernate jar复制StatisticsService和接口,使用相同的包,等等


这意味着它还可以与hibernate jconsole插件一起使用,这是一个不错的加号。:)

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

我从Hibernate 3.2.7中获取了旧的StatisticsServiceMBean,并将其用作模板,使用JMX注释包装Hibernate 5中的统计对象

使用jconsole hibernate统计插件

我确信如果你不使用Spring,你仍然可以做类似的事情

因此,如果您使用的是Spring,则不必键入所有这些内容,如下所示:

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

}

谢谢,刚刚试过,效果很好,非常感谢!嗨,Carlsen,我正在学习你两年前的解决方案。我让它工作了,但在JConsole中它会输出未能找到接口方法的可调用委托:public abstract long net.sf.hibernate.jconsole.stats.EntityStatistics.getDeleteCount()。你知道这里发生了什么吗?这个错误是在Hibernate 5.4.2JMX中修复的(没有spring)