Announcement Announcement Module
Collapse
No announcement yet.
Second level cache problem (ehcache)? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Second level cache problem (ehcache)?

    I am using Hibernate with ehcache as second level cache and it's working fine until I call session.refresh after I have saved an object. If I look at the log, HB tells me this..:

    Code:
    TwoPhaseLoad    - adding entity to second-level cache: [no.havleik.dt.model.OrdreNew#6343]
    08.04.2006 00:43:10,327 DEBUG ReadWriteCache  - Caching: no.havleik.dt.model.OrdreNew#6343
    08.04.2006 00:43:10,327 DEBUG EhCache         - key: no.havleik.dt.model.OrdreNew#6343
    08.04.2006 00:43:10,327 DEBUG ReadWriteCache  - Item was locked: no.havleik.dt.model.OrdreNew#6343
    08.04.2006 00:43:10,327 DEBUG TwoPhaseLoad    - done materializing entity [no.havleik.dt.model.OrdreNew#6343]
    As long as I don't call refresh after I save, I get no locks... (The reason why I call refresh, is because I am using triggers...)

    How can I unlock this object?


    Regards,

    BTJ

  • #2
    the cache lock is different from the session lock. If you change your cache from read write to read-only there will be no locking inside the cache.
    As for the session, you can unlock an object using by locking it in mode LockMode.NONE.

    Comment


    • #3
      Yes, but the problem is that after calling session.refresh the object is never found in the second level cache by hibernate anymore.. Before calling refresh, hibernate always finds the object in ehcache and no sql is run to fetch the object.

      So for some reason, calling refresh ruins the second level caching for that spesific object, why?


      BTJ

      Comment


      • #4
        Yes, but the problem is that after calling session.refresh the object is never found in the second level cache by hibernate anymore..
        When you call refresh, the entity and it's collections (AFAIK) are evicted from the cache. If you don't call evict anymore and the cache is populated then your object should be taken from the cache.
        Note that session.find does not uses the cache for the principal entity but causes the cache to be populated. What's your code snippet?

        Comment


        • #5
          Well, that's what I was expecting, but the object is never added to the cache again...

          Here is the code for save...:

          Code:
          getHibernateTemplate().saveOrUpdate(obj);
          getHibernateTemplate().flush();
          getHibernateTemplate().refresh(obj);
          and here is how I load the object..:

          Code:
          public Object retrieve(Class entityClass, Serializable id) throws ObjectNotFoundException
              {
                  Object obj = getHibernateTemplate().get(entityClass, id);
          
                  if (obj == null)
                  {
                      throw new ObjectNotFoundException("Object does not exist: class=" + entityClass.getName() + ", id=" + id);
                  }
          
                  return obj;
              }

          BTJ

          Comment


          • #6
            Why don't you use load - it will always use the cache (unlike get) and throws an exception in case you it doesn't find the object.

            Comment


            • #7
              The reason I don't use load, is because the exception is not thrown until you try to access the loaded object, which I find is a bit late...

              And also, the same thing with the second level cache happens when I use load() instead of get()...


              BTJ

              Comment


              • #8
                Have you tried also the HB forums? You should report this issue since it looks like it's a bug.

                Comment


                • #9
                  Yes, I have tried but I think the Hibernate forum is the worst forum I have ever tried using; no one answers any post there....

                  I am using Hibernate 3.0.5 now but I think I will try to upgrade to 3.1.3 and see if the behaviour changes...

                  BTJ

                  Comment


                  • #10
                    You can take a look at the tests within Hibernate - usually they clearify some of the functionality.

                    Comment


                    • #11
                      Yes, I could...

                      Btw, I tried with HB 3.1.3 but with the same result...


                      BTJ

                      Comment

                      Working...
                      X