Is there a way to get a mongodb connection id?

1,381 views
Skip to first unread message

Mark M

unread,
Aug 31, 2010, 7:36:28 AM8/31/10
to mongodb-user
Hi All,

Is there any way to get information on the connections to mongodb? It
looks like mongodb keeps track of when and what number a particular
connection to the database is. I am looking for a way to create/get a
unique connection id.
I was thinking that if I could get hold of that information through
pymongo, I could use it to lock my documents on a per connection
basis.

Get document.
Get connection ids to mongodb
Check if document (lock field) contains a connection id that is
currently using mongodb
If so the document is locked
If not the document is not locked

My issue is that the findandmodify command doesn’t mark the document
in any way. Therefore multiple threads may work on the same document
and only find out about it at the end. I do not want more than one
thread working on the document at a time. I want to handle the case
when those threads crash, and fail to unlock the document. Since I do
not know how long a thread will want to lock a document, a timestamp
will not work.

Mark

Michael Dirolf

unread,
Aug 31, 2010, 9:26:58 AM8/31/10
to mongod...@googlegroups.com
I don't think there is currently any way to get an ID like that. I
guess I'm not sure why you can't use findAndModify to do locking
though? Query for an unlocked document and in the update set the lock
field.

> --
> 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.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Eliot Horowitz

unread,
Aug 31, 2010, 9:30:32 AM8/31/10
to mongod...@googlegroups.com
You can do this:

> db.runCommand( "whatsmyuri" )
{ "you" : "127.0.0.1:39232", "ok" : 1 }


What are you trying to do?  findandmodify is thread safe though - not sure what it doesn't do that you need.


On Tue, Aug 31, 2010 at 7:36 AM, Mark M <markj...@gmail.com> wrote:

Mark M

unread,
Sep 1, 2010, 3:51:56 AM9/1/10
to mongodb-user
What happens if the thread never unlocks the document? This has come
up because my python process has been crashing before the document is
unlocked. Therefore the document stays locked forever. Normally a lock
is unlocked when the lock goes out of scope. In C++ that would mean
that the function exited. Because I have to explicitly unlock the
document in the thread that locked it, if that thread dies, I have no
way to unlock it anymore.

Findandmondify is great, but it is more of a way to handle thread
collisions, then locking. If processing a document is very quick, then
the method works great. If processing a single document takes an hour,
you obviously want to minimize collisions.

Unlike most systems, I potentially will have threads on different
computers all vying to modify the same document.

I simply need a way to find out whether or not the thread that locked
the document crashed before it unlocked it, or is still alive and
processing the document. Since the threads will be on different
computers, the threads all have to communicate through the database.
Another way to do this is through a polling method, Each thread
updates a field in the database every 10 seconds. If more than 10
seconds pass between document updates, then you know that thread is
dead, and any locks it has on documents are no longer valid. That is a
common solution, just not very elegant.

Mark

On Aug 31, 3:26 pm, Michael Dirolf <m...@10gen.com> wrote:
> I don't think there is currently any way to get an ID like that. I
> guess I'm not sure why you can't use findAndModify to do locking
> though? Query for an unlocked document and in the update set the lock
> field.
>

Mark M

unread,
Sep 1, 2010, 8:01:30 AM9/1/10
to mongodb-user
Hi Eliot,

That is perfect!
Now do you know of a way to get all of the open connections on a
mongodb?
If there is a runCommand for that, I think I could do what I want to
do with the locking!

Thanks,
Mark

On Aug 31, 3:30 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> You can do this:
>
> > db.runCommand( "whatsmyuri" )
>
> { "you" : "127.0.0.1:39232", "ok" : 1 }
>
> What are you trying to do?  findandmodify is thread safe though - not sure
> what it doesn't do that you need.
>
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsu...@googlegroups.com>
> > .

Eliot Horowitz

unread,
Sep 1, 2010, 8:47:21 AM9/1/10
to mongod...@googlegroups.com
db.$cmd.sys.inprog.findOne( {$all : 1 } )


To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages