Skip to content

Commit 2da2536

Browse files
committed
JAVA-2811: Stop supporting client sessions for standalone servers
The driver sessions specification requires that client sessions not be supported when connected to a MongoDB standalone server. This patch ensures that explicitly created client sessions are not supported, and that implicit sessions are not used, when connected to a standalone server.
1 parent 2f43461 commit 2da2536

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

driver-async/src/main/com/mongodb/async/client/ClientSessionHelper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import com.mongodb.async.SingleResultCallback;
2222
import com.mongodb.connection.ClusterConnectionMode;
2323
import com.mongodb.connection.ClusterDescription;
24+
import com.mongodb.connection.ClusterType;
2425
import com.mongodb.connection.Server;
2526
import com.mongodb.connection.ServerDescription;
27+
import com.mongodb.connection.ServerType;
2628
import com.mongodb.internal.session.ServerSessionPool;
2729
import com.mongodb.lang.Nullable;
2830
import com.mongodb.selector.ServerSelector;
@@ -59,7 +61,8 @@ void createClientSession(final ClientSessionOptions options, final OperationExec
5961
} else {
6062
ClusterDescription clusterDescription = mongoClient.getCluster().getCurrentDescription();
6163
if (!getServerDescriptionListToConsiderForSessionSupport(clusterDescription).isEmpty()
62-
&& clusterDescription.getLogicalSessionTimeoutMinutes() != null) {
64+
&& clusterDescription.getLogicalSessionTimeoutMinutes() != null
65+
&& clusterDescription.getType() != ClusterType.STANDALONE) {
6366
callback.onResult(createClientSession(options, executor), null);
6467
} else {
6568
mongoClient.getCluster().selectServerAsync(new ServerSelector() {
@@ -72,7 +75,8 @@ public List<ServerDescription> select(final ClusterDescription clusterDescriptio
7275
public void onResult(final Server server, final Throwable t) {
7376
if (t != null) {
7477
callback.onResult(null, null);
75-
} else if (server.getDescription().getLogicalSessionTimeoutMinutes() == null) {
78+
} else if (server.getDescription().getLogicalSessionTimeoutMinutes() == null
79+
|| server.getDescription().getType() == ServerType.STANDALONE) {
7680
callback.onResult(null, null);
7781
} else {
7882
callback.onResult(createClientSession(options, executor), null);

driver-async/src/test/functional/com/mongodb/async/client/MongoClientSessionSpecification.groovy

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
5252
thrown(IllegalArgumentException)
5353
}
5454

55-
@IgnoreIf({ serverVersionAtLeast(3, 6) })
55+
@IgnoreIf({ serverVersionAtLeast(3, 6) && !isStandalone() })
5656
def 'should throw MongoClientException starting a session when sessions are not supported'() {
5757
when:
5858
startSession(ClientSessionOptions.builder().build())
@@ -61,7 +61,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
6161
thrown(MongoClientException)
6262
}
6363

64-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
64+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
6565
def 'should create session with correct defaults'() {
6666
when:
6767
def options = ClientSessionOptions.builder().build()
@@ -85,7 +85,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
8585
clientSession.close()
8686
}
8787

88-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
88+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
8989
def 'cluster time should advance'() {
9090
given:
9191
def firstOperationTime = new BsonTimestamp(42, 1)
@@ -129,7 +129,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
129129
clientSession.close()
130130
}
131131

132-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
132+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
133133
def 'operation time should advance'() {
134134
given:
135135
def firstOperationTime = new BsonTimestamp(42, 1)
@@ -170,7 +170,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
170170
clientSession.close()
171171
}
172172

173-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
173+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
174174
def 'methods that use the session should throw if the session is closed'() {
175175
given:
176176
def options = ClientSessionOptions.builder().build()
@@ -199,7 +199,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
199199
clientSession.close()
200200
}
201201

202-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
202+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
203203
def 'informational methods should not throw if the session is closed'() {
204204
given:
205205
def options = ClientSessionOptions.builder().build()
@@ -216,7 +216,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
216216
true
217217
}
218218

219-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
219+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
220220
def 'should apply causally consistent session option to client session'() {
221221
when:
222222
def clientSession = startSession(ClientSessionOptions.builder().causallyConsistent(causallyConsistent).build())
@@ -232,7 +232,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
232232
causallyConsistent << [true, false]
233233
}
234234

235-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
235+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
236236
def 'client session should have server session with valid identifier'() {
237237
given:
238238
def clientSession = startSession(ClientSessionOptions.builder().build())
@@ -251,7 +251,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
251251
clientSession.close()
252252
}
253253

254-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
254+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
255255
def 'should use a default session'() {
256256
given:
257257
def commandListener = new TestCommandListener()
@@ -270,7 +270,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
270270
client?.close()
271271
}
272272

273-
@IgnoreIf({ serverVersionAtLeast(3, 6) })
273+
@IgnoreIf({ serverVersionAtLeast(3, 6) && !isStandalone() })
274274
def 'should not use a default session when sessions are not supported'() {
275275
given:
276276
def commandListener = new TestCommandListener()

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
4848
thrown(IllegalArgumentException)
4949
}
5050

51-
@IgnoreIf({ serverVersionAtLeast(3, 6) })
51+
@IgnoreIf({ serverVersionAtLeast(3, 6) && !isStandalone() })
5252
def 'should throw MongoClientException starting a session when sessions are not supported'() {
5353
when:
5454
getMongoClient().startSession()
@@ -63,7 +63,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
6363
thrown(MongoClientException)
6464
}
6565

66-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
66+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
6767
def 'should create session with correct defaults'() {
6868
expect:
6969
clientSession.getOriginator() == getMongoClient()
@@ -86,7 +86,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
8686
getMongoClient().startSession(ClientSessionOptions.builder().build())]
8787
}
8888

89-
@IgnoreIf({ !serverVersionAtLeast(3, 7) })
89+
@IgnoreIf({ !serverVersionAtLeast(3, 7) || isStandalone() })
9090
def 'should use mutated client write concern for default transaction options'() {
9191
given:
9292
def originalWriteConcern = getMongoClient().getWriteConcern()
@@ -106,7 +106,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
106106
clientSession?.close()
107107
}
108108

109-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
109+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
110110
def 'cluster time should advance'() {
111111
given:
112112
def firstOperationTime = new BsonTimestamp(42, 1)
@@ -150,7 +150,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
150150
clientSession?.close()
151151
}
152152

153-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
153+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
154154
def 'operation time should advance'() {
155155
given:
156156
def firstOperationTime = new BsonTimestamp(42, 1)
@@ -191,7 +191,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
191191
clientSession?.close()
192192
}
193193

194-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
194+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
195195
def 'methods that use the session should throw if the session is closed'() {
196196
given:
197197
def options = ClientSessionOptions.builder().build()
@@ -220,7 +220,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
220220
clientSession?.close()
221221
}
222222

223-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
223+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
224224
def 'informational methods should not throw if the session is closed'() {
225225
given:
226226
def options = ClientSessionOptions.builder().build()
@@ -240,7 +240,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
240240
clientSession?.close()
241241
}
242242

243-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
243+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
244244
def 'should apply causally consistent session option to client session'() {
245245
when:
246246
def clientSession = getMongoClient().startSession(ClientSessionOptions.builder()
@@ -258,7 +258,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
258258
causallyConsistent << [true, false]
259259
}
260260

261-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
261+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
262262
def 'client session should have server session with valid identifier'() {
263263
given:
264264
def clientSession = getMongoClient().startSession(ClientSessionOptions.builder().build())
@@ -277,13 +277,13 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
277277
clientSession?.close()
278278
}
279279

280-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
280+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
281281
def 'should use a default session'() {
282282
given:
283283
def commandListener = new TestCommandListener()
284284
def optionsBuilder = MongoClientOptions.builder()
285285
.addCommandListener(commandListener)
286-
def client = new MongoClient(Fixture.getMongoClientURI(optionsBuilder))
286+
def client = new MongoClient(getMongoClientURI(optionsBuilder))
287287

288288
when:
289289
client.getDatabase('admin').runCommand(new BsonDocument('ping', new BsonInt32(1)))
@@ -297,7 +297,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
297297
client?.close()
298298
}
299299

300-
@IgnoreIf({ serverVersionAtLeast(3, 6) })
300+
@IgnoreIf({ serverVersionAtLeast(3, 6) && !isStandalone() })
301301
def 'should not use a default session when sessions are not supported'() {
302302
given:
303303
def commandListener = new TestCommandListener()

driver-sync/src/main/com/mongodb/client/internal/MongoClientDelegate.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.mongodb.connection.Cluster;
3333
import com.mongodb.connection.ClusterConnectionMode;
3434
import com.mongodb.connection.ClusterDescription;
35+
import com.mongodb.connection.ClusterType;
3536
import com.mongodb.connection.ServerDescription;
3637
import com.mongodb.internal.session.ServerSessionPool;
3738
import com.mongodb.lang.Nullable;
@@ -83,7 +84,12 @@ public ClientSession createClientSession(final ClientSessionOptions options, fin
8384
return null;
8485
}
8586

86-
if (getConnectedClusterDescription().getLogicalSessionTimeoutMinutes() != null) {
87+
ClusterDescription connectedClusterDescription = getConnectedClusterDescription();
88+
89+
if (connectedClusterDescription.getType() == ClusterType.STANDALONE
90+
|| connectedClusterDescription.getLogicalSessionTimeoutMinutes() == null) {
91+
return null;
92+
} else {
8793
ClientSessionOptions mergedOptions = ClientSessionOptions.builder(options)
8894
.defaultTransactionOptions(
8995
TransactionOptions.merge(
@@ -94,8 +100,6 @@ public ClientSession createClientSession(final ClientSessionOptions options, fin
94100
.build()))
95101
.build();
96102
return new ClientSessionImpl(serverSessionPool, originator, mergedOptions, this);
97-
} else {
98-
return null;
99103
}
100104
}
101105

driver-sync/src/test/functional/com/mongodb/client/MongoClientSessionSpecification.groovy

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.mongodb.client
1919
import category.Slow
2020
import com.mongodb.ClientSessionOptions
2121
import com.mongodb.MongoClientException
22+
import com.mongodb.MongoClientSettings
2223
import com.mongodb.ReadConcern
2324
import com.mongodb.ReadPreference
2425
import com.mongodb.TransactionOptions
@@ -52,7 +53,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
5253
thrown(IllegalArgumentException)
5354
}
5455

55-
@IgnoreIf({ serverVersionAtLeast(3, 6) })
56+
@IgnoreIf({ serverVersionAtLeast(3, 6) && !isStandalone() })
5657
def 'should throw MongoClientException starting a session when sessions are not supported'() {
5758
when:
5859
getMongoClient().startSession()
@@ -67,7 +68,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
6768
thrown(MongoClientException)
6869
}
6970

70-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
71+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
7172
def 'should create session with correct defaults'() {
7273
expect:
7374
clientSession.getOriginator() == getMongoClient()
@@ -90,7 +91,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
9091
getMongoClient().startSession(ClientSessionOptions.builder().build())]
9192
}
9293

93-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
94+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
9495
def 'cluster time should advance'() {
9596
given:
9697
def firstOperationTime = new BsonTimestamp(42, 1)
@@ -131,7 +132,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
131132
clientSession.getClusterTime() == secondClusterTime
132133
}
133134

134-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
135+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
135136
def 'operation time should advance'() {
136137
given:
137138
def firstOperationTime = new BsonTimestamp(42, 1)
@@ -169,7 +170,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
169170
clientSession.getOperationTime() == secondOperationTime
170171
}
171172

172-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
173+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
173174
def 'methods that use the session should throw if the session is closed'() {
174175
given:
175176
def options = ClientSessionOptions.builder().build()
@@ -195,7 +196,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
195196
thrown(IllegalStateException)
196197
}
197198

198-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
199+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
199200
def 'informational methods should not throw if the session is closed'() {
200201
given:
201202
def options = ClientSessionOptions.builder().build()
@@ -212,7 +213,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
212213
noExceptionThrown()
213214
}
214215

215-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
216+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
216217
def 'should apply causally consistent session option to client session'() {
217218
when:
218219
def clientSession = getMongoClient().startSession(ClientSessionOptions.builder()
@@ -227,7 +228,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
227228
causallyConsistent << [true, false]
228229
}
229230

230-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
231+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
231232
def 'client session should have server session with valid identifier'() {
232233
given:
233234
def clientSession = getMongoClient().startSession(ClientSessionOptions.builder().build())
@@ -243,7 +244,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
243244
identifier.getBinary('id').data.length == 16
244245
}
245246

246-
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
247+
@IgnoreIf({ !serverVersionAtLeast(3, 6) || isStandalone() })
247248
def 'should use a default session'() {
248249
given:
249250
def commandListener = new TestCommandListener()
@@ -262,7 +263,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
262263
client?.close()
263264
}
264265

265-
@IgnoreIf({ serverVersionAtLeast(3, 6) })
266+
@IgnoreIf({ serverVersionAtLeast(3, 6) && !isStandalone() })
266267
def 'should not use a default session when sessions are not supported'() {
267268
given:
268269
def commandListener = new TestCommandListener()

0 commit comments

Comments
 (0)