Contextual Sessions in Hibernate

Somehow I missed this new feature in Hibernate 3.0

Contextual sessions means that you can ask the SessionFactory for the current session. It will then return the session associated with your current JTA transaction. Of course, you need to have a JTA transaction, but that’s not that big a deal. (For example, transactions with EJBs are JTA based)

This means that you don’t need to pass the current session around in order to get access to it; you simply ask the SessionFactory. Of course, you need to get at the session factory – that can be done via JNDI very easily. And, of course, the session factory is threadsafe, whereas the session isn’t.

One thought on “Contextual Sessions in Hibernate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s