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

Driver validates it is the creator of a session before using it

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Session Management
    • None
    • Not Needed

      Server sessions can be reused by the same database user who created them even across different connections and applications, but the Java driver validates that any session reference passed in is from the same MongoClient:

      Exception: java.lang.IllegalStateException: state should be: ClientSession from same MongoClient

      Source: https://github.com/mongodb/mongo-java-driver/blob/d57dfa8af686180239058a06eca838e18bfe5cdd/driver-sync/src/main/com/mongodb/client/internal/MongoClientDelegate.java#L259

      There are circumstances where 2 different connections are using the same MongoClient and using the same session. (We are using this case for integrity testing of some replica set operations involving IP resolution changes, where 1 mongoclient is connected to an old IP via a proxy, and 1 is connected to a new IP via a proxy).

      I have tried to work around this validation by using runCommand() with an explicit `lsid` operation like so:

      final Document result =
        pClient
          .getDatabase("test")
          .runCommand(
            new Document("find", "test")
            .append("sort", new BsonDocument("i", new BsonInt32(1)))
            }}{{.append("batchSize", 0)
            }}{{.append("lsid", _sessionIdent));

      But it appears in this case, the driver implicitly creates a new session and assigns a second `lsid` property, causing this server duplicate bson field error:

      com.mongodb.MongoCommandException: Command failed with error 40413 (Location40413): 'BSON field 'APIParametersFromClient.lsid' is a duplicate field' on server xxx-hostname-here-xxx:27017. The full response is {"ok": 0.0, "errmsg": "BSON field 'APIParametersFromClient.lsid' is a duplicate field", "code": 40413, "codeName": "Location40413", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1615894777, "i": 1, "signature": {"hash":

      {"$binary": "XQL3Tqirs9PAzLVPXsuKQHdxE4E=", "$type": "00"}

      , "keyId": {"$numberLong": "6939936267161305090"}}}, "operationTime": {"$timestamp":

      {"t": 1615894777, "i": 1}

      }}}}

       

      2 questions:

      • Is there some way we can make use of a session ID created outside the MongoClient performing the operation, without driver code changes?
      • It seems the server supports the operation, is the validation in question is superfluous and can it be removed?

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            jack.wearden@mongodb.com Jack Wearden
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: