views:

99

answers:

1

We are using Hibernate and ehcache as 2nd level cache.

If I load an entity that is cached (e.g. cache-usage="read-write") and update it, it seems that this immediately results in an SQL UPDATE.

(How) Can I influence when this SQL UPDATE happens?

        hibSession = HibernateUtil.getReadWriteSession();
        tx = hibSession.beginTransaction();
        User u = (User) hibSession.load(User.class, user_id);
        u.modify();
        hibSession.update(u);
        tx.commit();

Edit: It seems that setting a CacheMode should have an effect, but each hibSession.update() results in an immediate SQL UPDATE, regardless which CacheMode I set.

A: 

If I load an entity that is cached (e.g. cache-usage="read-write") and update it, it seems that this immediately results in an SQL UPDATE.

This seems to be a very good thing to me.

It seems that setting a CacheMode should have an effect, but each hibSession.update() results in an immediate SQL UPDATE, regardless which CacheMode I set.

SQL UPDATEs are performed when the session is flushed (when the tx is committed here) and this is just the expected behavior. I don't see anything in the CacheMode that could change this and I really don't understand why you would like to change this behavior. I mean, when do you want the UPDATEs to be performed? Outside the transaction? I must be missing something. Can you clarify?


Update: It appears the question was about Write-Behind caching. So, quoting Terracotta's Hibernate Integration to clarify:

Write-Behind Caching

When you think of cache you will arrive at these cache strategies : Read-Through Caching, Write-Through Caching, Write-Behind Caching. Hibernate Second Level cache is Read-Write-Through Cache where if cache miss occurs, entity is read from database and then handed over to cache for susequent access. But H2LC is not Write-Behind caching. With Terracotta's disk persistence and asynchronsous module it would be really efficient for certain use-cases to implement write-behind. Currently Hibernate just directly writes to database. Instead if its modified to write to second level cache and persistent async-database-queue, this would decrease latency and increase throughput dramatically.

Write-Behind is just not how Hibernate currently works.

Pascal Thivent
i guess what i am thinking about is Write-Behand-caching, mentioned here: http://ehcache.org/documentation/write_through_caching.htmli'm not sure if that introduces other problems, of course, and i don't know whether that would be compatible with Hibernate (though i dont see any big reasons why it should not).
thomers
@thomers I see. But I don't know if this can work with Hibernate. What if the changes can't be made in the database because of optimistic locking for example? I must think about this deeper (I can see that Coherence, Infinispan, Ehcache does offer write Behind caching but I really wonder if this is supported by Hibernate).
Pascal Thivent