Controlling when SQL UPDATE is executing on ehcache 'd classes
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(开发者_Python百科) results in an immediate SQL UPDATE, regardless which CacheMode I set.
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.
精彩评论