Tuesday, September 19, 2006

Hibernate session.

Hibernate session management is a little tricky for new users.
There are two ways to obtain a session:
sessionFactory.getCurrentSession(); and

The first way requires the following line in the hibernate.cfg.xml:
<property name="current_session_context_class">thread</property>

Seems if you use the first method, when you commit a transaction by calling tx.commit(), the session will be closed automatically. This can introduce error if you try to close the session again.

Whereas if you use the second method, a transaction will not close the session after commit, you will have the chance to manually close the session.

The above comments applies for hibernate 3.1, I used it with Tomcat5.5 datasource on Java1.4