Domanda

I would like to acquire exclusive locks either with mongo or couch db.

  1. Is it possible? (API reference would be helpful)
  2. Is it possible that exclusive lock gets released when client suddenly dies?
È stato utile?

Soluzione

Regarding MongoDB:

Mongo does not support it out of the box and is very limited when it comes to concurrency stuff. To briefly get a feeling of what is supported and what's not, please refer to mogo FAQ on concurrency. This is pretty informative to me.

But you can always fake it. Create a collection with an object to manage you exclusive lock. Then use findAndModify to set isLocked field to true. Don't forget to add query option to look for object with isLocked = false. If a object is returned it means you have the lock, if not the object is already locked.

db.lockCollection.findAndModify( {
   query: { isLocked: false },
   update: { $set: { isLocked: true } },
   upsert: true
} );

To avoid infinite locking due to a failure you can create a TTL index which will remove the document after some arbitrary time period. The main disadvantages of the approach is that it is a custom one, so it might have bugs and the lock is actually on your application side but not on the db one, so any other code can do anything.

Hope it helps!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top