Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4944

Consider refactoring the public API such that we don't do IO in constructors and constructor methods

    • Type: Icon: Improvement Improvement
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: API
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            valentin.kovalenko@mongodb.com Valentin Kavalenka
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: