-
Type: Improvement
-
Resolution: Works as Designed
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: API
-
None
This ticket was created as a result of https://github.com/mongodb/mongo-java-driver/pull/1104#discussion_r1160728842. The linked discussion has my detailed initial thoughts on the issue.
In short, doing IO in constructors / constructor methods is usually a bad idea:
- The usual and reasonable expectation of a programmer is that creating an object just creates the object, and does not result in downloading anything from the Internet or launching missiles. I.e., people don't usually guard object creation with retry logic in case it fails. This leads to the next item.
- As mentioned in the SDAM spec, if a constructor does network IO, like our ConnectionString or MongoClients.create, then this may result in failing to restart an application when there networking issues, even if the application was working just fine despite those issues.
- Constructors are synchronous, which means that the IO they do as part of constructing an object must be done synchronously.
If we explore this and decide to change our API, we at first must deprecate the existing API in 4.x.
P.S. I discovered that while the SDAM summary forbids doing any IO when constructing a client, SDAM makes an exception for initial DNS seed list discovery when given a connection string with mongodb+srv scheme. This is surprising, and I can't think of a good justification on my own, but even more surprising is that given this exception, neither document makes an exception for the hostname resolution into an IP address, which is also potentially an IO operation (not necessarily network IO). Regardless of this SDAM exception, doing IO when constructing ConnectionString lead us to the unfortunate API that burdens a user with making sure that the DnsClient set via MongoClientSettings must be the same as the one passed to the ConnectionString constructor.