Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not quite sure, but:</p> <blockquote> <p>It allows concurrent read access to elements already in the cache. If the element is null, other reads will block until an element with the same key is put into the cache.</p> </blockquote> <p>Doesn't it means that Hibernate would wait until some other thread places the object into cache? That's what you observe, right?</p> <p>Hib and cache works like this:</p> <ol> <li>Hib gets a request for an object</li> <li>Hib checks if the object is in cache -- cache.get()</li> <li>No? Hib loads the object from DB and puts into cache -- cache.put()</li> </ol> <p>So if the object is not in cache (not placed there by some previous update operation), Hib would wait on 1) forever.</p> <p>I think you need a cache variant where the thread only waits for an object for a short time. E.g. 100ms. If the object is not arrived, the thread should get null (and thus Hibernate will load the object from DB and place into the cache).</p> <p>Actually, a better logic would be:</p> <ol> <li>Check that another thread is requesting the same object</li> <li>If true, wait for long (500ms) for the object to arrive</li> <li>If not true, return null immediately</li> </ol> <p>(We cannot wait on 2 forever, as the thread may fail to put the object into cache -- due to exception).</p> <p>If BlockingCache doesn't support this behaviour, you need to implement a cache yourself. I did it in past, it's not hard -- main methods are get() and put() (though API apparently has grown since that).</p> <p><strong>UPDATE</strong></p> <p>Actually, I just read the sources of BlockingCache. It does exactly what I said -- lock and wait for timeout. Thus you don't need to do anything, just use it... </p> <pre><code>public Element get(final Object key) throws RuntimeException, LockTimeoutException { Sync lock = getLockForKey(key); Element element; acquiredLockForKey(key, lock, LockType.WRITE); element = cache.get(key); if (element != null) { lock.unlock(LockType.WRITE); } return element; } public void put(Element element) { if (element == null) { return; } Object key = element.getObjectKey(); Object value = element.getObjectValue(); getLockForKey(key).lock(LockType.WRITE); try { if (value != null) { cache.put(element); } else { cache.remove(key); } } finally { getLockForKey(key).unlock(LockType.WRITE); } } </code></pre> <p>So it's kind of strange it doesn't work for you. Tell me something: in your code this spot:</p> <pre><code>Ehcache cache = manager.getEhcache("foo"); </code></pre> <p>is it synchronized? If multiple requests come at the same time, will there be only one instance of cache?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload