Neron Neron - 1 month ago 14
Java Question

Coherence lock-unlock usage

I have a test sample about coherence lock-unlock mechanism like this:

public class Test {


public static void main(String[] args) throws InterruptedException, IOException, IllegalArgumentException, IllegalAccessException {

Trt test=new Trt();
test.lock();
Thread a=new Thread(test);
a.start();
}

public static class Trt implements Runnable{

NamedCache cache=null;

@Override
public void run() {

System.out.println(cache.unlock("asd"));


}

public void lock(){
cache= CacheFactory.getCache(Globals.REGISTRY_CACHE_NAME);

System.out.println(cache.lock("asd"));

}

}
}


So the result is:

true
false


The result that I am expecting is:

true
true


But the case is, I have only one item 'test', I am all using it and it has only one cache instance in it. So the owner of the cache is that cache instance.

Why is it not able to close it and returns
false
in the end?

Thanks

Ali

Answer

From Oracle Coherence Developer's Guide:

When a lock is in place, it is the responsibility of the caller (either in the same thread or the same cluster node, depending on the lease-granularity configuration) to release the lock.

By default Coherence uses thread ownership granularity, so that is probably the reason lock is not being released.

A value of thread means that locks are held by a thread that obtained them and can only be released by that thread. A value of member means that locks are held by a cluster node and any thread running on the cluster node that obtained the lock can release it.

See http://docs.oracle.com/cd/E24290_01/coh.371/e22837/api_transactionslocks.htm#BEIIEEBB and http://docs.oracle.com/cd/E24290_01/coh.371/e22837/appendix_operational.htm#BAGJBCEF for more details.