java driver - threading q

4 views
Skip to first unread message

Liam Staskawicz

unread,
Apr 18, 2009, 4:04:05 PM4/18/09
to mongod...@googlegroups.com
Hi - I've been playing with the Java driver and mostly things have
been very smooth. I'm just getting into a situation now where I'm
testing some concurrent access and seeing a few things I didn't expect.

My strategy is to create a single Mongo() instance which is accessed
by all threads - I don't provide any locking around it, perhaps that's
naive. Once I've done this, I'm reading from it from multiple threads
via findOne() for my tests at the moment. The strange thing is that I
see multiple 'connection accepted' messages in the mongod console, and
then eventually I get some OOM errors like

java.lang.OutOfMemoryError: Direct buffer memory
at java.nio.Bits.reserveMemory(Bits.java:633)
at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:95)
at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
at com.mongodb.ByteEncoder.<init>(ByteEncoder.java:118)
at com.mongodb.ByteEncoder.<init>(ByteEncoder.java:47)
at com.mongodb.ByteEncoder$1.createNew(ByteEncoder.java:106)
at com.mongodb.ByteEncoder$1.createNew(ByteEncoder.java:109)
at com.mongodb.util.SimplePool._get(SimplePool.java:140)
at com.mongodb.util.SimplePool.get(SimplePool.java:106)
at com.mongodb.util.SimplePool.get(SimplePool.java:95)
at com.mongodb.ByteEncoder.get(ByteEncoder.java:92)
at com.mongodb.DBApiLayer$MyCollection.find(DBApiLayer.java:397)
at com.mongodb.DBCollection.findOne(DBCollection.java:245)

Is it expected to have multiple connections to the database when only
one Mongo() is instantiated? Is this even the right approach? Any
thoughts what might cause the above trace? Thanks for any hints.

Liam

Eliot

unread,
Apr 18, 2009, 4:17:24 PM4/18/09
to mongod...@googlegroups.com
A single Mongo instance will have multiple connections to the
database, and do connection pooling and management for you.

This allows you to interact it with from multiple threads without
locking, which is totally safe.

Each connection does have overhead though, so you probably have more
connections open than your max memory allows.

We can probably make it smarter about how many connections to allow
based on memory size, but you probably should just increase your max
memory a bit.

-Eliot

Liam Staskawicz

unread,
Apr 18, 2009, 4:42:05 PM4/18/09
to mongod...@googlegroups.com
Works like a charm with more memory. I'll add a feature request to
jira to make a note of the issue. Thanks for the quick response as
usual.

Liam
Reply all
Reply to author
Forward
0 new messages