I'm trying to write a transaction-safe way of performing a lazy load of an 
object from the database if it doesnt exist. However as IgniteCache doesn't 
have the equivalent of HashMap.computeIfAbsent, I'm trying to use the 
IgniteCache.lock method to achieve this.

The question is whether IgniteCache.lock will lock the cache for that key even 
if <b>the key does not yet exist in the cache</b>.

Following is the example code,

public GridOrder getOrder(OrderKey key) {
        Lock orderKeyLock = cache.lock(key);
        try {
            orderKeyLock.lock();
            if (!cache.containsKey(key)) {
                GridOrder order = loadOrderFromDb(key);
                if ( order == null) {
                    throw new IllegalStateException("Key " + key + " not in 
Order Cache or in Database DB Name ");
                }
                cache.put(key,order);
            }
            return cache.get(key);
        } finally {
            orderKeyLock.unlock();
        }
    }

Alternatively, is there a better way to achieve this?

_________________________________________________________

This email, its contents, and any attachments transmitted with it are intended 
only for the addressee(s) and may be confidential and legally privileged. We do 
not waive any confidentiality by misdelivery. If you have received this email 
in error, please notify the sender immediately and delete it. You should not 
copy it, forward it or otherwise use the contents, attachments or information 
in any way. Any liability for viruses is excluded to the fullest extent 
permitted by law.

Tudor Capital Europe LLP (TCE) is authorised and regulated by The Financial 
Conduct Authority (the FCA). TCE is registered as a limited liability 
partnership in England and Wales No: OC340673 with its registered office at 10 
New Burlington Street, London, W1S 3BE, United Kingdom

Reply via email to