Skip to content

Commit

Permalink
Check out an implicit session only after checking out a connection (#865
Browse files Browse the repository at this point in the history
)

This is accomplished by delaying allocation of a server session until actually required.

JAVA-4365
  • Loading branch information
jyemin authored Feb 9, 2022
1 parent 178a0d4 commit dd291c3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class BaseClientSessionImpl implements ClientSession {
private static final String CLUSTER_TIME_KEY = "clusterTime";

private final ServerSessionPool serverSessionPool;
private final ServerSession serverSession;
private ServerSession serverSession;
private final Object originator;
private final ClientSessionOptions options;
private BsonDocument clusterTime;
Expand All @@ -46,7 +46,6 @@ public class BaseClientSessionImpl implements ClientSession {

public BaseClientSessionImpl(final ServerSessionPool serverSessionPool, final Object originator, final ClientSessionOptions options) {
this.serverSessionPool = serverSessionPool;
this.serverSession = serverSessionPool.get();
this.originator = originator;
this.options = options;
this.pinnedServerAddress = null;
Expand Down Expand Up @@ -120,6 +119,9 @@ public BsonTimestamp getOperationTime() {
@Override
public ServerSession getServerSession() {
isTrue("open", !closed);
if (serverSession == null) {
serverSession = serverSessionPool.get();
}
return serverSession;
}

Expand Down Expand Up @@ -179,7 +181,9 @@ private BsonTimestamp greaterOf(final BsonTimestamp newOperationTime) {
public void close() {
if (!closed) {
closed = true;
serverSessionPool.release(serverSession);
if (serverSession != null) {
serverSessionPool.release(serverSession);
}
clearTransactionContext();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.internal.session;

import com.mongodb.ClientSessionOptions;
import com.mongodb.session.ClientSession;
import org.junit.jupiter.api.Test;

import static com.mongodb.ClusterFixture.getCluster;
import static org.junit.jupiter.api.Assertions.assertEquals;

class BaseClientSessionImplTest {

@Test
void shouldNotCheckoutServerSessionIfNeverRequested() {
ServerSessionPool serverSessionPool = new ServerSessionPool(getCluster(), null);
ClientSession clientSession = new BaseClientSessionImpl(serverSessionPool, new Object(), ClientSessionOptions.builder().build());

assertEquals(0, serverSessionPool.getInUseCount());

clientSession.close();

assertEquals(0, serverSessionPool.getInUseCount());
}

@Test
void shouldDelayServerSessionCheckoutUntilRequested() {
ServerSessionPool serverSessionPool = new ServerSessionPool(getCluster(), null);
ClientSession clientSession = new BaseClientSessionImpl(serverSessionPool, new Object(), ClientSessionOptions.builder().build());

assertEquals(0, serverSessionPool.getInUseCount());

clientSession.getServerSession();

assertEquals(1, serverSessionPool.getInUseCount());

clientSession.close();

assertEquals(0, serverSessionPool.getInUseCount());
}
}

0 comments on commit dd291c3

Please sign in to comment.