RSS 2.0 New Entries Syndication Feed Atom 0.3 New Entries Syndication Feed

Show blog menu v



Use it



Sibling projects

RIFE powered

Valid XHTML 1.0 Transitional

Valid CSS!

Blogs : Archives

< First recordings of my new band ... check it out!   My first YouTube video : Robert Plant cover song >
How do you use Hibernate Extended Sessions?

The Open Session in View pattern is very popular when Hibernate is used in a web application. This allows you to safely use managed entities when your view is being rendered. At Terracotta, we're currently researching what other patterns are popular for Hibernate and how people apply them.

One that I'm in particular looking into is the Extended Session pattern for long running conversations. This allows you to disconnect a Hibernate session in between requests and to store it in the HTTP session in the meantime.

I'd like to know who's using the extended session pattern and what you use it for.

To understand more about this, I developed a small example application to get a feel for the advantages, the benefits, the gotchas and the surprises. The application can be found below, it is inspired by snippets from Hibernate tutorials and example applications:

The example uses only servlets, filters and Hibernate. It is a webapp that allows you to create events and add people to it over different requests. When an event has been created with all the relevant people, it can be committed in one step. The intermediate state is kept within the managed entities and the Hibernate session until it's flushed.

This is what I found worth mentioning:

  • Whenever a transaction is created and committed, it doesn't matter if Hibernate flush mode is set to manual, certain operations like persist and merge will be propagated to the database at transaction commit. However, modifications to managed entities wont be committed, this can be confusing.
  • Even with manually flushed sessions, transactions are always real database transactions, causing locks to be held. They thus need to be as short as possible.
  • A single general-purpose servlet filter might not be feasible, depending on how transactions should behave. In this application, I ended up creating only a transaction around the actual session flush.
  • Data that is present in a manually flushed session buffer is batched but not available for queries.
  • This doesn't behave the same as a database transaction with isolation levels that can still see their own changes.
  • If the data needs to be available for a query, a commit needs to happen first.
  • This can require application changes, like a redirect after form submission in case transactions are automatically committed through a servlet filter.

Totally unrelated but cool, you can use an AnnotationConfiguration class to setup the Hibernate config and use a fluent interface to add the managed entity classes. This reduces the amount of XML and eases maintenance (see HibernateUtil class in my example).

posted by Geert Bevin in Java on Apr 16, 2009 3:53 PM : 4 comments [permalink]


Re: How do you use Hibernate Extended Sessions?
On the first point, persist() only provokes a flush if it is needed to obtain the entity's ID. Using the Sequence generation strategy, for instance, does not cause an immediate flush.

Another issue that I observed is that if the extended session first persists an entity to the session and then builds it incrementally, in the end the flush will yield an INSERT followed by an UPDATE. This can be problematic if there are integrity constraints that are only satisfied by the complete object.
Re: How do you use Hibernate Extended Sessions?
AFAIK, JBoss Seam use extended session heavily and transparently in their Conversation Context. you should check it out
Re: How do you use Hibernate Extended Sessions?
There are so many gotchas that I've become weary of using extended. Many of these problems aren't even consistent within Hibernate itself, but are functions on the backend database and how the db performs locking, provides sequences/auto increment columns, etc.

I've found 'temporary' tables to be a good solution at times. That way your real db tables are not at risk of being polluted with incomplete data, while the previously mentioned issues go away.
Re: How do you use Hibernate Extended Sessions?
Thanks a lot for the additional insights everyone, much appreciated!

Add a new comment

Comments on this blog entry have been closed.

< First recordings of my new band ... check it out!   My first YouTube video : Robert Plant cover song >
Google web