Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Second level cache problem (ehcache)?

  1. #1

    Default 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
    Someone wrote:
    "I understand that if you play a Windows CD backwards you hear strange Satanic messages"
    To which someone replied:
    "It's even worse than that; play it forwards and it installs Windows"

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3

    Default

    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
    Someone wrote:
    "I understand that if you play a Windows CD backwards you hear strange Satanic messages"
    To which someone replied:
    "It's even worse than that; play it forwards and it installs Windows"

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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?
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5

    Default

    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
    Someone wrote:
    "I understand that if you play a Windows CD backwards you hear strange Satanic messages"
    To which someone replied:
    "It's even worse than that; play it forwards and it installs Windows"

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  7. #7

    Default

    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
    Someone wrote:
    "I understand that if you play a Windows CD backwards you hear strange Satanic messages"
    To which someone replied:
    "It's even worse than that; play it forwards and it installs Windows"

  8. #8
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Have you tried also the HB forums? You should report this issue since it looks like it's a bug.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  9. #9

    Default

    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
    Someone wrote:
    "I understand that if you play a Windows CD backwards you hear strange Satanic messages"
    To which someone replied:
    "It's even worse than that; play it forwards and it installs Windows"

  10. #10
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    You can take a look at the tests within Hibernate - usually they clearify some of the functionality.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •