Announcement Announcement Module
No announcement yet.
Seldom occurrence of Hibernate LazyInitializationException in JSF application Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Seldom occurrence of Hibernate LazyInitializationException in JSF application

    Hello everybody,

    in our JSF application, users sometimes get a LazyInitializationException which I can't reproduce.

    The application stack is as follows:

    JSF 2.0 (Richfaces 4) Datatable iterates over a collection.

    The collection is obtained from a view backing bean (Spring 3.0.5, annotated with @Component and @Scope("session") ), entitymanager is injected here:
    @PersistenceContext(type=PersistenceContextType.EX TENDED)
    private EntityManager entityManager;
    While the data loading method is annotated with @Transactional, the plain collection getter isn't.

    OpenEntityManagerInViewFilter is used.

    There are cases where a data bean object refers to its own data, triggering lazy loading (in RdiLnb.getLatestStatusChanges).
    This does always work when I test it, but sometimes an exception is thrown:

    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.bayer.application.entity.RdiLnb.rdiLnbStatusch anges, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationException(AbstractPersi
    at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationExceptionIfNotConnected (
    at org.hibernate.collection.AbstractPersistentCollect ion.initialize( 68)
    at org.hibernate.collection.AbstractPersistentCollect
    at org.hibernate.collection.PersistentBag.toArray(Per
    at java.util.ArrayList.<init>(
    at com.bayer.application.entity.RdiLnb.getLatestStatu sChanges(
    at com.bayer.application.entity.RdiLnb.statusChange(R
    at com.bayer.application.entity.RdiLnb.getLatestStatu sChange_Opened(
    at sun.reflect.GeneratedMethodAccessor1370.invoke(Unk nown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De
    at java.lang.reflect.Method.invoke(
    at javax.el.BeanELResolver.getValue(BeanELResolver.ja va:302)
    ... (the stack trace shows that OpenEntityManagerInViewFilter was applied)
    And sometimes, the exception is:
    org.hibernate.LazyInitializationException: illegal access to loading collection
    which I don't understand, because this isn't (supposed to be) multithreading code. I could synchronize it to be sure.

    Since RdiLnb isn't proxied by Spring, it probably makes no sense to annotate one of the three RdiLnb methods above with @Transactional. In my experience, having too many getters annotated with @Transactional can be a performance hit - for each roundtrip to the backing bean, a connection is obtained from the pool and probably even commited.

    I'd appreciate any ideas why the LazyInitializationException appears in rare situations only.

    From what I've read in this forum, it might be a good idea to write a new servlet filter and handle transactions directly ( ). Or maybe it is a good idea to have an EntityManager live for the lifetime of a session instead of a request? There should be a 'standard' way of solving this typical problem...

    Thanks and regards,