This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Spring stopped keeping session alive in lazy initialization

Aug 16th, 2004, 08:32 PM

I had this working last week under a slightly different configuration and now I can't repeat what I did. Basically, I am trying to lazy initialize collections and it's complaining : "Failed to lazily initialize a collection - no session or session was closed " Before, Spring was taking care of this for me fine, even when I used "getHibernateTemplate().find()". Can someone take a look? Greatly appreciated...I just can't look at this anymore today.

Comment

One thing though, these are just my unit tests. I actually will be using SLSBs to maintain the session there per each CMT method that is called where I want to use lazy init. inside a particular call. Will I have to front-end my DAOs with these code bits or insert them in each SLSB method call? I thought Spring manages the session-per-transaction bit automatically.

Comment

Now the HibernateTemplate needs to have the allowCreate flag set to true, so that the first usage will create a new Session, which will get synchronized to the CMT tx, and get used subsequently.

Since I have my persistent code in DAO POJOs and I have no way of knowing which one will be called first, where would I initially create the session (i.e., set allowCreate to true)? Originally, I thought Spring takes care of this for you, but from your response it sounds like I need to initially demarcate the session once the CMT is invoked (i.e., inside the SLSB method).

It seems like I hear conflicting things on this. I just want to put this one to rest, so please any information on this would be extremely grateful! :wink:

Thanks,
Lou

Comment

I'm just saying that the HibernateTemplate you are using needs to have the allowCreate flag set to true. This means the (Spring) code will see that there is currently no Session bound to the current thread/TX, and ensure that one is created and bound to the thread and synched to the tx. After that it will be used for subsequent usages of HibernateTemplate with the same SessionFactory, with no new session being created.

The flag is there so that in the cases where people are using HibernateTransactionManager, or alternately JTATransactionManager+HibernateInterceptor, they can turn if off and ensure that DAOs always assume a session is already available, which it should be in those cases. In your case, the allowCreate=true is needed to allow the usage in the DAO to force the creation. But there is nothing you have to do other than use the template.

Regards,

Comment

Thanks Colin...you know I think the difference is that I'm not using HibernateTransaction or JtaTransactionManager in applicationContext.xml - I'm using the Hibernate look-up manager class, per the following, in hibernate.cfg.xml:

Rereading what Jurgeon had said on the topic, this allows a Hibernate session to automatically attach to the CMT. I configured this by turning on log4j DEBUG and sure enough, the session "keeps alive" till I see that transaction commit. I see now that I'm have a different issue, which I thought was originally a result of the transaction manager. I'll post that separately.