Java access to MongoDb Lock/Unlock Concurrency

605 views
Skip to first unread message

Han Chen

unread,
Mar 18, 2014, 3:58:15 PM3/18/14
to mongod...@googlegroups.com
I read some articles and they tell me MongoDb has Lock/Unlock Concurrency feature itself. So for example I have 2 Java instances access to one MongoDb to read & update data, each of them does the same thing:

    1. Query all the qualified data from MongoDb  [read]
       (using DBCursor cursor = collection.find(query))
    2. Do some calculation and update/write back to MongoDb (maybe all of them or 
       one of them) [write]

I want to make sure all the qualified data which query from the MongoDb should be "locked" because they might be updated/changed later; if the other Java instance accesses to the same data at the same time, it will return the wrong result. So all of them must be accessed by only one instance at the same time until the update/write process is done (Step 2). 

How does MongoDb handle this situation above? Does it have some features to achieve it itself automatically or I have to do it manually (using other application such as zookeeper)? 

Justin Lee

unread,
Mar 18, 2014, 4:24:45 PM3/18/14
to mongod...@googlegroups.com
You will have to manage that concurrency yourself.


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Han Chen

unread,
Mar 18, 2014, 4:29:13 PM3/18/14
to mongod...@googlegroups.com
OK. Do you have advise about how to manage this concurrency?

Karl Yang

unread,
Mar 20, 2014, 10:22:14 PM3/20/14
to mongod...@googlegroups.com
Maybe you can set all your operations going through only one object, and give this object a lock.

Asya Kamsky

unread,
Mar 22, 2014, 3:43:14 PM3/22/14
to mongodb-user
One possible way is to set a field on a document when you are editing it, and then make the save conditional on that field not changing *or* don't allow another thread to read this object for editing if that field is set (but be careful of processes that start editing the object but never finish).

Asya

Reply all
Reply to author
Forward
0 new messages