[jboss-user] [JBoss Tools] New message: "Hibernate tools provides Hibernate statistics in Eclipse?"
Arbi Sookazian
do-not-reply at jboss.com
Tue Feb 9 00:35:15 EST 2010
User development,
A new message was posted in the thread "Hibernate tools provides Hibernate statistics in Eclipse?":
http://community.jboss.org/message/524938#524938
Author : Arbi Sookazian
Profile : http://community.jboss.org/people/asookazian
Message:
--------------------------------------------------------------
Does Hibernate tools provide Hibernate statistics in Eclipse (perhaps in the Hibernate console)?
I am currently using a JMX MBean to get dynamic data on Hibernate statistics for my EntityManager.
@Name("hibernateUtils")
@AutoCreate
public class HibernateUtils {
@In EntityManager entityManager;
public Statistics getStatistics() {
return ((HibernateSessionProxy)entityManager.getDelegate()).getSessionFactory().getStatistics();
}
public SessionFactory getSessionFactory() {
return ((HibernateSessionProxy)entityManager.getDelegate()).getSessionFactory();
}
}
@Name("jmxHibernateStatistics")
@AutoCreate
public class JmxHibernateStatistics {
@In HibernateUtils hibernateUtils;
@Logger Log log;
public void installHibernateMBean() {
Statistics stats = hibernateUtils.getStatistics();
stats.logSummary();
log.info("new Date(stats.getStartTime()) = "+new Date(stats.getStartTime()));
String[] myQueries = stats.getQueries();
for(int i = 0; i < myQueries.length; i++){
log.info("myQueries["+i+"]: "+myQueries[i]);
}
//get your SF
SessionFactory sf = hibernateUtils.getSessionFactory();
//get the available MBean servers
List list = MBeanServerFactory.findMBeanServer(null);
//take the first one
MBeanServer server = (MBeanServer) list.get(0);
//build the MBean name
ObjectName on = null;
try{
on = new ObjectName("Hibernate:type=statistics,application=sample");
}
catch(MalformedObjectNameException e){
log.error(e);
}
StatisticsService mBean = new StatisticsService();
mBean.setSessionFactory(sf);
try {
server.registerMBean(mBean, on);
} catch (InstanceAlreadyExistsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MBeanRegistrationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotCompliantMBeanException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//WARNING:
/*
* When the SessionFactory is closed, we need to unregister the MBean
//get the available MBean servers
ArrayList list = MBeanServerFactory.findMBeanServer(null);
//take the first one
MBeanServer server = (MBeanServer) list.get(0);
server.unregisterMBean(on);
You can add this code to you HibernateUtil like class.
*/
}
}
--------------------------------------------------------------
To reply to this message visit the message page: http://community.jboss.org/message/524938#524938
More information about the jboss-user
mailing list