4 Replies Latest reply on Oct 31, 2008 4:10 AM by daniel jarrett

    EJB3 and Entity Caching

    sunnyyfl Newbie

      I have create a simple program based on the CachedEntityRun.java sample from the distributed documentation on EJB3. All entity beans in my same app. use the transactional caching strategy (@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL).

      When the app is running and trying to retrieve an entity via its primary ID from the database, I can see the cache is filled with the value from the objected just retrieved. So the 'caching' part is working but when I try the second time to retireve the same persistence instance using the same primary ID, I detect hibernate is generating SQL select statements to the database. Thus instead of trying to fetch from the cache, it still connecting to the db to retrieve the record.

      Does anyone has similar experience? If you have a working sample, please shine some light on me. Thanks.

        • 1. Re: EJB3 and Entity Caching
          JBoss User Newbie

          I am having the same problem.

          I am using @FlushMode(FlushModeType.COMMIT) above each method inside of my Stateless Session Bean.



          Inside one of my methods I am trying create a query and get a result set.
          @Entity
          @Cache(...TRANSACTIONAL)
          public class Rock {

          @Id..auto
          int id;

          }

          @Stateless
          public class RockUpdater {

          @FlushMode(FlushModeType.NEVER)
          private List getList() {
          //Get all the rocks...
          getResultList()...///This is taking forever... and db is being hit
          why is it not being pulled out of cache???????????????????????
          }

          @FlushMode(FlushModeType.COMMIT)
          public void update(List updates) {
          ...
          getList()
          ...update some of the rocks entity beans...
          if not in the list.
          create new rock and call rock.persist()
          }

          }

          @Service
          public class JoMBean{
          @EJB
          RockUpdater ru;

          ..//within a TimerTask...
          ru.update(List moldspores);
          }


          I tried to use Stateful (EXTENDED Entitymanager) but the darn thing was passivating during active method calls from a service. Says it timed out. I was in the middle of a method call... how could it time out?

          • 2. Re: EJB3 and Entity Caching
            Ales Justin Master

            Regarding caching ... you should probably implement hashCode and equals on entities.

            • 3. Re: EJB3 and Entity Caching
              JBoss User Newbie

              Still having the same problem. Is this a bug?

              • 4. Re: EJB3 and Entity Caching
                daniel jarrett Newbie

                I'm finding exactly the same problem. Does anyone know a solution to this?