1 Reply Latest reply on Aug 27, 2008 8:34 AM by zilbi

    EJB3 transaction / lazy loading issue

    zilbi

      Hi,

      got an entity with a few lazy collections in it.
      also got a stateless session bean that uses this entity in an ejb method. the method does no changes to the entity, only reads the entity's data and returns it.

      now if i use:

      @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)

      i get the notorious lazy load exception problem when i try to invoke getters of the entity's lazy collection. so i remove the attribute at all or use the
      @TransactionAttribute(TransactionAttributeType.NOT_REQUIRES_NEW)
      attribute instead.

      the thing is that i then see in the server log that hibernate is invoking an update query on the entity. the query updates all the entity's fields (not the lazy collections) by primary key field as well as version field.
      the update SQL together with commit takes around 20-40 milliseconds which is just too much for me.

      why is the update being performed?
      is there a way to prevent it (since i know for sure i do not need it)?
      is there a way to enjoy lazy load without opening a transaction? (a sort of a read only trx..)

      thanks,
      zilbi

        • 1. Re: EJB3 transaction / lazy loading issue
          zilbi

          solved it!

          it turns out that sorting the collection returned from one of the entity's getters (using collections.sort) make hibernate think i changed the entity.

          i cloned the collection before sorting and it is solved - no more update query when the transaction exits.

          cool!