Locking and unlocking documents

I’m running Couchbase 3.0 with the couchbase==2.0.7 library.

Is there a way to keep the document locked after you do the upsert?

rv = self.cb.lock("locked_key", ttl=5) # <-- document is locked
new_value = rv.value.upper()
cb.upsert("locked_key", new_value, rv.cas) # <-- document is unlocked

My problem is that I have more than one upsert on a locked document and if there are two threads that started executing the code in the same time, when thread1 does the first upsert the document is unlocked and thread2 aquires the lock, but the thread1 is not finished and at the next upsert an error will be raised.

Using upsert() will implicitly unlock the item.

Can I ask why you are using lock?

I have a custom locking mechanism. Do you propose anything else instead?