Concurrency strategy configuration for JBoss TreeCache as 2nd level Hibernate cache?

Isolation level is a tricky issue. For example, if you set @Cache(usage = CacheConcurrencyStrategy. TRANSACTIONAL) for an entity but configure NONE as IsolationLevel for the JBossTreecache, what happens?

Mostly, hard-to-find bugs in production... You should understand that by using read-write cache you essentially mire yourself in distributed transactions with all their 'niceties'. Ok, about combinations: Read-only cache setting in bernate should be used when your objects do not change. For example, for a country dictionary.

Cache concurrency level NONE or READ_ONLY should be used with it. Non-strict-read-write should be used when your cached objects change, but that happens rarely and chances for race conditions are small. For example, for a timezone dictionary - timezones might appear/disappear occasionally, but that happens may be a couple times a year.

Again, cache concurrency level NONE or READ_ONLY should be used with it. Now, to more interesting combinations. Transactional caches in bernate assumes that cache updates are transactional but does nothing to ensure it.So you MUST use a full-blown external XA (distributed transactions) coordinator, and you really really really do not want it unless you really really know what you're doing.

Most likely, you'll have to use the full EJB3 container for XA-manager support though it's possible to use external transaction manager like http://www.atomikos.com/ with plain servlets + Spring. Obviously, you need to use TRANSACTIONAL caches with it.'READ_WRITE` is an interesting combination. In this mode bernate itself works as a lightweight XA-coordinator, so it doesn't require a full-blown external XA.

Short description of how it works: In this mode bernate manages the transactions itself. All DB actions must be inside a transaction, autocommit mode won't work. During the flush() (which might appear multiple time during transaction lifetime, but usually happens just before the commit) bernate goes through a session and searches for updated/inserted/deleted objects.

These objects then are first saved to the database, and then locked and updated in the cache so concurrent transactions can neither update nor read them. If the transaction is then rolled back (explicitly or because of some error) the locked objects are simply released and evicted from the cache, so other transactions can read/update them. If the transaction is committed successfully, then the locked objects are simply released and other threads can read/write them.

There are couple of fine points here: Possible repeatable read violation. Imagine that we have Transaction A (tA) and Transaction B (tB) which start simultaneously and both load object X, tA then modifies this object and then tA is committed. In a lot of databases which use snapshot isolation (Oracle, PostgreSQL, FireBird), if tB requests object X again it should receive the same object state as in the beginning of the transaction.

However, READ_WRITE cache might violate this condition - there's no snapshot isolation there. Bernate tries to work around it by using timestamps on cached objects but on OSes with poor timer resolution (15.6ms on Windows) it is guaranteed to let some races slip through. Possible optimistic stale object versions - it IS possible to get stale object versions if you're very unlucky to work on Windows, and have several transactions commit with the same timestamp.

Thanks for putting effort into answering my question. A few things are not clear to me however: You write that "NONE or READ_ONLY should be used with it". Are you really referring to JBossTreeCache here?

I can't find READ_ONLY IsolationLevel in JBossTreeCache. I also thought that NONSTRICT_READ_WRITE and READ_WRITE bernate usage is not supported by JBossTreeCache. Can you comment on that?

– Steve Feb 23 at 21:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions