Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Sunday, November 20, 2011

Enabling JMX on Hibernate.

Here are some instructions for making the Hibernate Statistics JMX MBean available from JBoss.
The example below has been tested with JDK 1.6 and JBoss 4.x.

Then, if you are using Spring, add this to your applicationContext.xml file:



    <bean id="jmxExporter"
        class="org.springframework.jmx.export.MBeanExporter">
        <property name="beans">
            <map>
                <entry key="Hibernate:name=statistics">
                    <ref local="statisticsBean" />
                </entry>
            </map>
        </property>
    </bean> 

    <bean id="statisticsBean" class="org.hibernate.jmx.StatisticsService">
        <property name="statisticsEnabled">
            <value>true</value>
        </property>
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

and then set the hibernate.generate_statistics property in your applicationContext-hibernate.xml file:
 <prop key="hibernate.generate_statistics">true</prop>
 

When you browse the MBeans (using JConsole.exe - which lives in the bin dir of your JDK 1.5 
distribution), you should see Hibernate, if you double click on that node, you should see
the Statistics Bean.


Below are a couple of screenshots.


If you are not using Spring, inside StartupListener.java (inside contextInitialized method) add:


import org.hibernate.jmx.*; 
import javax.management.*;
import java.lang.management.ManagementFactory;

try {
    SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
    MBeanServer mbeanServer =
        ManagementFactory.getPlatformMBeanServer();
    ObjectName on =
        new ObjectName("Hibernate:type=statistics,application=appfuse");
    StatisticsService mBean = new StatisticsService(); 
    mBean.setStatisticsEnabled(true);
    mBean.setSessionFactory(sessionFactory);
    mbeanServer.registerMBean(mBean, on);
}
catch (Exception e) {
    log.error("Error registering Hibernate StatisticsService [" + e.getMessage() + "]", e);
}
 

After starting up JBoss, you should be able to attach to the server using JConsole (port 9002, as in the config above). From there, you should see the Hibernate MBean where you can view the statistics that have been collected.

Thursday, August 18, 2011

How to Map Dates with Hibernate – Use joda-time

This is always a question – how to map temporal data in our hibernate entities – whether to use java.util.Date, java.util.Calendar or simply long. The correct answer is: neither of these. Use joda-time – the de-facto Java datetime API. Using it throughout the whole project is a no-brainer, but how to use it with hibernate – you can’t use @Temporal. Hibernate supports custom types, so there’s a little project that solves the issue – joda-time – hibernate support.
The user guide is pretty clear:

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime fromDate;
However, there’s one issue with that library – it uses static final fields from hibernate, which requires recompilation if the version of hibernate changes (see here). For that reason you may need to extend the PersistentDateTime class, override the 2 methods that use the static final TIMESTAMP field, and copy the exact same code from the superclass.
Using joda-time throughout the whole project will save tons of headaches. So I strongly suggest the above mechanism to use joda-time in your hibernate entities as well.