[JAVA-760] Document that MongoClient doesn't throw an exception on connection failure Created: 17/Feb/13  Updated: 28/Jan/19  Resolved: 28/Jan/19

Status: Closed
Project: Java Driver
Component/s: Documentation
Affects Version/s: 2.10.1
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Oliver Bock Assignee: Ross Lawley
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

When creating a new MongoClient instance with valid host/port parameters, but no MongoDB running there, I would expect a MongoException to be thrown. This is not the case. I can even use the MongoClient instance to get a DB instance via getDB() and subsequently a collection instance via getCollection(), again without being actually connected and any exception. I also tried getConnector().isOpen() to verify whether the connection was established and it returned true, without a running server. The latter issue might be me misinterpreting the API though.

Others (see user "litiales") are observing this as well.



 Comments   
Comment by Ross Lawley [ 28/Jan/19 ]

I'm going to close this ticket as "Won't Fix".

With distributed systems there is a level of complexity with regards to what being "connected" means.  MongoDB is a highly available and scalable database, it is designed to be able to handle failures that typically occur in distributed systems. This is why we have concepts such as WriteConcern and readPreference, allowing a user to specify what is acceptable when it comes to any given operation.

The driver has worked this way for 10 years and while it may come as a surprise to some users that the driver doesn't immediately report connectivity issues, the benefits of the pattern have proven themselves over time. There are various monitoring APIs that users have found useful with regards to understanding more about connection status - these can be found with the [main documentation| monitoring aois.

Ross

Comment by Oliver Bock [ 21/Feb/13 ]

I don't think we're going to change this behavior, but I agree that it should be documented.

Agreed, the behaviour is ok when you know what to expect, and when.

Thanks.

Comment by Jeffrey Yemin [ 20/Feb/13 ]

I don't think we're going to change this behavior, but I agree that it should be documented.

This is probably obvious, but a simple thing to do to force a connection would be something like:

MongoClient client = new MongoClient(...);
client.getDB("admin").command("ping").throwOnError();

Comment by Oliver Bock [ 17/Feb/13 ]

After all this seems more like a documentation issue than a bug.
Anyway, documenting this issue here might help others running into the same pitfall.

Comment by Oliver Bock [ 17/Feb/13 ]

Update: I do get an exception as soon as I try to actually use the obtained DB instance for an online operation like dropDatabase(), so it seems the connection is established lazily. While I can understand this from a resource management point of view it seems rather unintuitive, in particular because the hostname is seemingly checked immediately as MongoClient throws an UnknownHostException. The same is true for connect(), which is just an alias for getDB(), because its name somewhat implies a connection is actually established when calling it.

Generated at Thu Feb 08 08:53:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.