getAndLock in JAVA

Dear All,

I’m currently exploring document locking using “getAndLock(“Key”, timeOut)” method, but unfortunately i’m not able to lock the document. Below is the piece of the code i have written:

=================================================================
List uris = new LinkedList();
uris.add(URI.create(CouchbaseURL));
CouchbaseClient client = null;
try {
client = new CouchbaseClient(uris,BucketName, “”);
} catch (IOException e) {
System.err.println("IOException connecting to Couchbase: " + e.getMessage());
System.exit(1);
}
Record = client.getAndLock(Key,30).toString();
Record1 = client.get(Key).toString();
client.shutdown();

In ideal case the statement “Record1 = client.get(Key).toString();” should not return the document. But in my case it is returning the document associated with the “key”. Event i’m able to execute the “client.set()” on the same key without getting any error (before releasing the lock and before 30 seconds). I’m not sure where exactly i’m going wrong, Kindly assist me to achieve locking on documents.

Thanks,
Siva

I’m using

  1. Java SDK 1.1.1
  2. Couchbase Server 2.0.1

Can some one please share your thoughts on this issue, where exactly i’m going wrong?

Thanks,
Siva

Hi,

there is one assumption wrong here: the documents you lock are just write locks and never read locks. So the code you posted here is expected to work correctly.

Can you share the code with writing where you also assume something is wrong?

Btw, we always recommend to use CAS updates instead since they are much more performant, getwithlock only has very distinct use cases. Can you shed some light on what you are trying to achieve here?