1

I am developing a web application that has multiple spring contexts. It has a main context that holds business logic, hibernate session and application's core needs, and other contexts are for spring mvc binding. Normally application works fine and everything, but when i refresh main context and try to reach hibernate session from other contexts, hibernate session throws this exception:

org.hibernate.HibernateException: No Session found for current thread at         
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at 
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:980)...

But funny thing is application can do startup initiation which includes selecting lots of data from db.

Is there a way that i can refresh spring context safe and sound?

P.S: I can get other spring context objects with no problem. And application works fine with multiple contexts until i refresh main context. And i tried refreshing the mvc context, from which i call hibernate session, after main context. Still the same exception in that mvc context.

Masterhead
  • 53
  • 6

1 Answers1

0

The reason you are getting that error is that you are whacking the Hibernate sessions out from underneath the threads. The sessions, from what I remember, associated with the Thread that created them. This is why you can't pass a proxied Hibernate entity to frameworks like BlazeDS. As soon as the proxied entity leaves the thread, the session is no long associated with it and you loose the ability to go after the items that are being proxied.

One solution might be to not use Proxies, and eagerly-load all relationships. This would be undermining one of the main benefits of using an ORM, however. Or, if you could somehow re-start all the threads in questions, making new threads that are associated with the new Hibernate session(s), that might work too.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79
  • Ok but here is the problem: I don't create my entities in spring context, they are created in hibernate session and no ties with the spring context. If the thread is destroyed and created then hibernate session and entities are created in new thread. And i tried context refreshing before it is supposed to work in multiple spring contexts. – Masterhead Apr 16 '13 at 06:15