Quickfix: Hibernate integration for ZK applications

If you’re using the awesome ZK framework together with Hibernate you might run into some issues where Hibernate behaves a little funky while the application runs.

The trouble’s cause

According to the developers of ZK, the reason for this is ZK’s multi-threaded event model. Whenever an event gets fired, a new event thread is created. This will lead to multiple instances of the Hibernate session class (one per thread) and thus lead to unpredictable behavior.

The quick fix

If you don’t want to read the whole blah-blah in the linked article, here is the quick fix. Just insert the following lines into your zk.xml file and you should be fine:

<!-- Hibernate SessionFactory life cycle -->
<listener>
<description>Hibernate SessionFactory life cycle</description>
<listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
</listener>

<!-- Hibernate OpenSessionInView life cycle -->
<listener>
<description>Hibernate Open Session In View life cycle</description>
<listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
</listener>

Follow me on social media

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.