Weighted session error in lazy sample

The error failed to lazily initialize a collection, no session or session was closed.

Many people ask almost the same question, but the resolution is not so intuitive. And, I think, one more question needs to be posed in order to describe the strange error message:

(I will not paste the source here because it is too long.)

DEBUG [main] (AbstractPlatformTransactionManager.java:365) - Creating new transaction with name [com.bee32.plover.orm.feaCat.FeaturePlayer.tcList]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
DEBUG [main] (HibernateTransactionManager.java:493) - Opened new Session [org.hibernate.impl.SessionImpl@19006c9] for Hibernate transaction
DEBUG [main] (HibernateTransactionManager.java:523) - Not preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@19006c9]
DEBUG [main] (HibernateTemplate.java:397) - Found thread-bound Session for HibernateTemplate
Hibernate:    /* criteria query */ select 
    ...

ERROR [main] (LazyInitializationException.java:42) - failed to lazily initialize a collection, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
  at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
    ...
  at java.util.HashSet.<init>(HashSet.java:116)
    ...
  at org.hibernate.loader.Loader.list(Loader.java:2124)
    ...
  at org.springframework.orm.hibernate3.HibernateTemplate$5.doInHibernate(HibernateTemplate.java:590)
  ...

DEBUG [main] (HibernateTemplate.java:422) - Not closing pre-bound Hibernate Session after HibernateTemplate
DEBUG [main] (AbstractPlatformTransactionManager.java:843) - Initiating transaction rollback
DEBUG [main] (HibernateTransactionManager.java:672) - Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@19006c9]
DEBUG [main] (HibernateTransactionManager.java:734) - Closing Hibernate Session [org.hibernate.impl.SessionImpl@19006c9] after transaction

As you can see, when lazy-fetch starts, an error has occurred, a thread-bound session already exists, and it is not yet closed until the transaction rolls back.

So why is he reporting what he has no session or session was closed?

EDIT Related source:

@Transactional
public void tcList() {
    for (Cat cat : dao.list()) {
        System.out.println("Saved cat: " + cat);
    }
}
+3
source share
3 answers

, catList ? , , dao.list() , .

list.get(0).someGetter() dao.list() . . Cat , , getter, .

, Transactional dao.list(), . , , Propagation_Required. @Transactional dao.list(), ()/session.close() , catList , .

+2

, - -? , ?

0

This means that you are trying to load / initialize a collection out of session. Probably u should maintain the session until you finish working with ur ..... so you need to manage your session resource with some glue code ... ppl will offer to initialize the lazy collection in the installed object method. it makes no sense to use lazy sampling as a live sampling. Taking a session alive before surgery may be the best way.

0
source

All Articles