Prueba Documento
Tomado de la pàgina http://www.mongodb.org/display/DOCS/Java+Driver+Concurrency
Java Driver Concurrency
The Java MongoDB driver is thread safe. If you are using in a web servingenvironment, for example, you should create a single Mongo instance, and you can use it in every request. The Mongo object maintains an internal pool of connections to the database (default pool size of10). For every request to the DB (find, insert, etc) the java thread will obtain a connection from the pool, execute the operation, and release the connection. This means the connection (socket) usedmay be different each time.
Additionally in the case of a replica set with slaveOk option turned on, the read operations will be distributed evenly across all slaves. This means that within thesame thread, a write followed by a read may be sent to different servers (master then slave). In turns the read operation may not see the data just written since replication is asynchronous. If you wantto ensure complete consistency in a "session" (maybe an http request), you would want the driver to use the same socket, which you can achieve by using a "consistent request". Call requestStart()before your operations and requestDone() to release the connection back to the pool:
DB db...;
db.requestStart();
code....
db.requestDone();
DB and DBCollection are completely thread safe.In fact, they are cached so you get the same instance no matter what.
WriteConcern option for single write operation
Since by default a connection is given back to the pool after each request, youmay wonder how calling getLastError() works after a write. You should actually use a write concern like WriteConcern.SAFE instead of calling getLastError() manually. The driver will then callgetLastError() before putting the connection back in the pool.
DBCollection coll...;
coll.insert(..., WriteConcern.SAFE);
// is equivalent to
DB db...;
DBCollection coll...;
db.requestStart();...
Regístrate para leer el documento completo.