Skip to content

Commit d719076

Browse files
committed
JAVA-2683: Don't include a session identifier for unacknowledged write commands
1 parent 3341d97 commit d719076

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

driver-core/src/main/com/mongodb/connection/CommandMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private List<BsonElement> getExtraElements(final SessionContext sessionContext)
200200
if (sessionContext.getClusterTime() != null) {
201201
extraElements.add(new BsonElement("$clusterTime", sessionContext.getClusterTime()));
202202
}
203-
if (sessionContext.hasSession()) {
203+
if (sessionContext.hasSession() && responseExpected) {
204204
extraElements.add(new BsonElement("lsid", sessionContext.getSessionId()));
205205
}
206206
if (!isDefaultReadPreference(getReadPreference())) {

driver-core/src/test/unit/com/mongodb/connection/CommandMessageSpecification.groovy

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class CommandMessageSpecification extends Specification {
4747
def 'should encode command message with OP_MSG'() {
4848
given:
4949
def message = new CommandMessage(namespace, command, fieldNameValidator, readPreference,
50-
MessageSettings.builder().serverVersion(new ServerVersion(3, 6)).build())
50+
MessageSettings.builder().serverVersion(new ServerVersion(3, 6)).build(),
51+
responseExpected, null, null)
5152
def output = new BasicOutputBuffer()
5253

5354
when:
@@ -66,7 +67,7 @@ class CommandMessageSpecification extends Specification {
6667
if (sessionContext.clusterTime != null) {
6768
expectedCommandDocument.append('$clusterTime', sessionContext.clusterTime)
6869
}
69-
if (sessionContext.hasSession()) {
70+
if (sessionContext.hasSession() && responseExpected) {
7071
expectedCommandDocument.append('lsid', sessionContext.sessionId)
7172
}
7273

@@ -76,7 +77,7 @@ class CommandMessageSpecification extends Specification {
7677
getCommandDocument(byteBuf, messageHeader) == expectedCommandDocument
7778

7879
where:
79-
[readPreference, sessionContext] << [
80+
[readPreference, sessionContext, responseExpected] << [
8081
[ReadPreference.primary(), ReadPreference.secondary()],
8182
[
8283
Stub(SessionContext) {
@@ -98,7 +99,8 @@ class CommandMessageSpecification extends Specification {
9899
getClusterTime() >> new BsonDocument('clusterTime', new BsonTimestamp(42, 1))
99100
getSessionId() >> new BsonDocument('id', new BsonBinary([1, 2, 3] as byte[]))
100101
}
101-
]
102+
],
103+
[true, false]
102104
].combinations()
103105
}
104106

driver/src/test/functional/com/mongodb/MongoClientSessionSpecification.groovy

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,33 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
323323
client.getDatabase('admin').runCommand(new BsonDocument('ping', new BsonInt32(1)))
324324

325325
then:
326-
def pingCommandStartedEvent = commandListener.events.get(0)
327-
!(pingCommandStartedEvent as CommandStartedEvent).command.containsKey('lsid')
326+
def pingCommandStartedEvent = commandListener.events.get(0) as CommandStartedEvent
327+
!pingCommandStartedEvent.command.containsKey('lsid')
328328

329329
cleanup:
330330
Fixture.getMongoClient().getDB('admin').removeUser(sessionTestUserName)
331331
client?.close()
332332
}
333+
334+
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
335+
def 'should not use a session for an unacknowledged write'() {
336+
given:
337+
def commandListener = new TestCommandListener()
338+
def optionsBuilder = MongoClientOptions.builder()
339+
.addCommandListener(commandListener)
340+
def mongoClientURI = getMongoClientURI(optionsBuilder)
341+
def client = new MongoClient(mongoClientURI)
342+
343+
when:
344+
client.getDatabase(getDatabaseName()).getCollection(getCollectionName())
345+
.withWriteConcern(WriteConcern.UNACKNOWLEDGED)
346+
.insertOne(new Document())
347+
348+
then:
349+
def insertEvent = commandListener.events.get(0) as CommandStartedEvent
350+
!insertEvent.command.containsKey('lsid')
351+
352+
cleanup:
353+
client?.close()
354+
}
333355
}

0 commit comments

Comments
 (0)