[CSHARP-1670] Document the various connection modes Created: 16/May/16 Updated: 31/Mar/22 |
|
| Status: | Backlog |
| Project: | C# Driver |
| Component/s: | Documentation |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Anatoliy Koperin | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | neweng | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
I used only one slave server in connection settings from the configured replica set:
Test code:
I get an exception:
But if you specify another server name, the connection is established.
But it does not work if I connect to a single server:
How to connect with automatic mode to a variable number of servers? |
| Comments |
| Comment by Anatoliy Koperin [ 20/May/16 ] |
|
I understand everything. Thanks! |
| Comment by Craig Wilson [ 19/May/16 ] |
|
1. Not really. At the point where the exception is getting thrown, we don't know if this is the first connect. We are doing what asked and are unable to select a server, which the message indicates. 2. No, the timeout is required. If there wasn't a timeout, then the driver would just hang. It can be lowered or raised using the "serverSelectionTimeoutMS" connection string option. The default is 30 seconds. As mentioned, the best way to combat this is to be very clear about what type of cluster you are attempting to talk to. If you have done this, these exceptions will only occur under actual error conditions. |
| Comment by Anatoliy Koperin [ 19/May/16 ] |
|
Yes, this makes sence. But i still have 2 questions: |
| Comment by Craig Wilson [ 17/May/16 ] |
|
Gotcha, So, the behavior is very unambiguous. Given X, we'll always do Y. However, I've realize that we don't have this documented, so I'll convert this into a docs ticket. For now, I believe the below is what you are wanting. Simple summarization -> If you intend to connect to a replica set, regardless of how many servers you are using, you should tell us that, either using a replica set name (strongly advised) or simply by telling us it's a replica set. Longer -> You can see the code here: https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs When specifying only a single server in the connection string, automatic mode will allow you to connect. However, commands will only run against the primary for safety. In the case where you've connected to a secondary, commands like connectionStatus can't find a primary to talk to and will fail with a TimeoutException. If you have intended to connect to a secondary and want to run commands against it, you need to tell us that was what you intended by telling us to connect directly to it, using connect=direct in the connection string or ConnectionMode.Direct. In the case where you intend to connect to a replica set but only have a single server at the current time (or only want to list a single one on the connection string), you should either give us a replica set name using the "replicaSet" connection string option or specify ConnectionMode.ReplicaSet. This will ensure that we'll discover the primary when it comes online. Hope that makes sense. |
| Comment by Anatoliy Koperin [ 17/May/16 ] |
|
Thanks for the answer! With driver v1.10 I could connect to a replica set by specifying a secondary server only. We used this when we were removing old servers from the replica set and adding new - to simplify the migration and eliminate the necessity of config modification on all web servers. Of course it was an intermediate step, and we finished with correct configs with all database servers specified. But actually this is not the case - I am worried about ambiguous behavior of the driver v 2.2. |
| Comment by Craig Wilson [ 16/May/16 ] |
|
Hi Anatoliy,
Perhaps you could indicate more exactly the server configuration you are expecting to connect to and I can help you out. If you don't know, then that could be a problem with the way we are currently parsing things, which is admittedly on the safe side in order to protect users from doing something unexpected. Craig |