Skip to content

Commit dd291c3

Browse files
authored
Check out an implicit session only after checking out a connection (#865)
This is accomplished by delaying allocation of a server session until actually required. JAVA-4365
1 parent 178a0d4 commit dd291c3

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

driver-core/src/main/com/mongodb/internal/session/BaseClientSessionImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class BaseClientSessionImpl implements ClientSession {
3333
private static final String CLUSTER_TIME_KEY = "clusterTime";
3434

3535
private final ServerSessionPool serverSessionPool;
36-
private final ServerSession serverSession;
36+
private ServerSession serverSession;
3737
private final Object originator;
3838
private final ClientSessionOptions options;
3939
private BsonDocument clusterTime;
@@ -46,7 +46,6 @@ public class BaseClientSessionImpl implements ClientSession {
4646

4747
public BaseClientSessionImpl(final ServerSessionPool serverSessionPool, final Object originator, final ClientSessionOptions options) {
4848
this.serverSessionPool = serverSessionPool;
49-
this.serverSession = serverSessionPool.get();
5049
this.originator = originator;
5150
this.options = options;
5251
this.pinnedServerAddress = null;
@@ -120,6 +119,9 @@ public BsonTimestamp getOperationTime() {
120119
@Override
121120
public ServerSession getServerSession() {
122121
isTrue("open", !closed);
122+
if (serverSession == null) {
123+
serverSession = serverSessionPool.get();
124+
}
123125
return serverSession;
124126
}
125127

@@ -179,7 +181,9 @@ private BsonTimestamp greaterOf(final BsonTimestamp newOperationTime) {
179181
public void close() {
180182
if (!closed) {
181183
closed = true;
182-
serverSessionPool.release(serverSession);
184+
if (serverSession != null) {
185+
serverSessionPool.release(serverSession);
186+
}
183187
clearTransactionContext();
184188
}
185189
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.internal.session;
18+
19+
import com.mongodb.ClientSessionOptions;
20+
import com.mongodb.session.ClientSession;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static com.mongodb.ClusterFixture.getCluster;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
26+
class BaseClientSessionImplTest {
27+
28+
@Test
29+
void shouldNotCheckoutServerSessionIfNeverRequested() {
30+
ServerSessionPool serverSessionPool = new ServerSessionPool(getCluster(), null);
31+
ClientSession clientSession = new BaseClientSessionImpl(serverSessionPool, new Object(), ClientSessionOptions.builder().build());
32+
33+
assertEquals(0, serverSessionPool.getInUseCount());
34+
35+
clientSession.close();
36+
37+
assertEquals(0, serverSessionPool.getInUseCount());
38+
}
39+
40+
@Test
41+
void shouldDelayServerSessionCheckoutUntilRequested() {
42+
ServerSessionPool serverSessionPool = new ServerSessionPool(getCluster(), null);
43+
ClientSession clientSession = new BaseClientSessionImpl(serverSessionPool, new Object(), ClientSessionOptions.builder().build());
44+
45+
assertEquals(0, serverSessionPool.getInUseCount());
46+
47+
clientSession.getServerSession();
48+
49+
assertEquals(1, serverSessionPool.getInUseCount());
50+
51+
clientSession.close();
52+
53+
assertEquals(0, serverSessionPool.getInUseCount());
54+
}
55+
}

0 commit comments

Comments
 (0)