views:

433

answers:

1

When I try to use the ThreadLocalSessionContext in the following way:

Session hsession = HibernateUtils.getSession();
ThreadLocalSessionContext.bind(hsession);
// do stuff
hsession.close();

I do this for every single Struts Action. Is there something I am doing wrong, causing me to get the following error?

[ThreadLocalSessionContext] Already session bound on call to bind(); make sure you clean up your sessions!

I checked all my files that had ThreadLocalSessionContext.bind in them and made sure these sessions are explicitly closed. Is there a way to monitor when sessions are binded onto session factories?

Thanks!!

A: 

As it turns out, although the documentation indicates that "If close() is called on a session managed by this class, it will be automatically unbound." It is actually NOT automatic!

You have to explicitly call ThreadLocalSessionContext.unbind(sessionFactory) to unbind the session. Otherwise, even if a session gets closed, it still stays binded to the SessionFactory.

tomato