Announcement Announcement Module
Collapse
No announcement yet.
Seldom occurrence of Hibernate LazyInitializationException in JSF application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 stentCollection.java:383)
    at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationExceptionIfNotConnected (AbstractPersistentCollection.java:375)
    at org.hibernate.collection.AbstractPersistentCollect ion.initialize(AbstractPersistentCollection.java:3 68)
    at org.hibernate.collection.AbstractPersistentCollect ion.read(AbstractPersistentCollection.java:111)
    at org.hibernate.collection.PersistentBag.toArray(Per sistentBag.java:280)
    at java.util.ArrayList.<init>(ArrayList.java:131)
    at com.bayer.application.entity.RdiLnb.getLatestStatu sChanges(RdiLnb.java:330)
    at com.bayer.application.entity.RdiLnb.statusChange(R diLnb.java:365)
    at com.bayer.application.entity.RdiLnb.getLatestStatu sChange_Opened(RdiLnb.java:379)
    at sun.reflect.GeneratedMethodAccessor1370.invoke(Unk nown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    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 ( http://forum.springsource.org/showthread.php?t=67041 ). 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,
    Carsten
Working...
X