[JAVA-1993] Connection changes in 3.0 driver Created: 01/Oct/15  Updated: 11/Sep/19  Resolved: 01/Oct/15

Status: Closed
Project: Java Driver
Component/s: Connection Management
Affects Version/s: 3.0.2
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: Michael Buonaccorsi Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Pre-Production



 Description   

We have a new application using mongoDB 3.0. Our DBA group is seeing that when the application starts (using mongo-java-driver-3.0.1.jar in conjunction with morphia-0.111.jar) there are connections established between the secondary servers as well as the primary server. The driver appears to be maintaining the connections to the secondary servers which our DBA says he does not see in our existing applications connection to 2.6 mongodb does not have the same issue.

Is this a known behavior?



 Comments   
Comment by Jeffrey Yemin [ 01/Oct/15 ]

Yes, this is expected behavior. The Java driver has always worked this way, though it has always depended on how you construct your MongoClient. If you do it like this:

new MongoClient(new MongoClientURI("mongodb://server1")
// or
new MongoClient(new ServerAddress("server1"))

the driver establishes a "direct" connection to that particular server, even if it's a member of a replica set, and will not establish a connection to any other server in the replica set. If you do it like this instead:

new MongoClient(new MongoClientURI("mongodb://server1/?replicaset=myreplsetname")
// or
new MongoClient(new MongoClientURI("mongodb://server1,server2")
// or
new MongoClient(Arrays.asList(new ServerAddress("server1")))
// or
new MongoClient(Arrays.asList(new ServerAddress("server1"), new ServerAddress("server2"))))

the driver will establish a replica set connection, which involves connecting to all members of the replica set and dynamically discovering which one is the primary and which are secondaries. This is the recommended way of connecting to a replica set.

I hope this answers your question.

In the future, questions like this should be directed to the mongodb-users Google group, where a larger audience can address your questions.

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