Skip to content

Commit 6b5ffac

Browse files
committed
JAVA-2788: Add isImplicitSession property to SessionContext
1 parent 2da2536 commit 6b5ffac

File tree

11 files changed

+73
-1
lines changed

11 files changed

+73
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ private final class AsyncClientSessionContext extends ClientSessionContext imple
152152
}
153153

154154

155+
@Override
156+
public boolean isImplicitSession() {
157+
return ownsSession;
158+
}
159+
155160
@Override
156161
public boolean notifyMessageSent() {
157162
return clientSession.notifyMessageSent();

driver-async/src/test/unit/com/mongodb/async/client/ClientSessionBindingSpecification.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ class ClientSessionBindingSpecification extends Specification {
149149
0 * session.close()
150150
}
151151

152+
def 'owned session is implicit'() {
153+
given:
154+
def session = Mock(ClientSession)
155+
def wrappedBinding = createStubBinding()
156+
157+
when:
158+
def binding = new ClientSessionBinding(session, ownsSession, wrappedBinding)
159+
160+
then:
161+
binding.getSessionContext().isImplicitSession() == ownsSession
162+
163+
where:
164+
ownsSession << [true, false]
165+
}
166+
152167
private AsyncReadWriteBinding createStubBinding() {
153168
def cluster = Mock(Cluster) {
154169
selectServerAsync(_, _) >> {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public boolean hasSession() {
3636
return wrapped.hasSession();
3737
}
3838

39+
@Override
40+
public boolean isImplicitSession() {
41+
return wrapped.isImplicitSession();
42+
}
43+
3944
@Override
4045
public BsonDocument getSessionId() {
4146
return wrapped.getSessionId();

driver-core/src/main/com/mongodb/internal/connection/NoOpSessionContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public boolean hasSession() {
3838
return false;
3939
}
4040

41+
@Override
42+
public boolean isImplicitSession() {
43+
throw new UnsupportedOperationException();
44+
}
45+
4146
@Override
4247
public BsonDocument getSessionId() {
4348
throw new UnsupportedOperationException();

driver-core/src/main/com/mongodb/session/SessionContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public interface SessionContext {
3434
*/
3535
boolean hasSession();
3636

37+
/**
38+
* Returns true if the session is implicit, and false if the application started the session explicity.
39+
*
40+
* @return true if the session is implicit
41+
* @since 3.8
42+
*/
43+
boolean isImplicitSession();
44+
3745
/**
3846
* Gets the session identifier if this context has a session backing it.
3947
*

driver-core/src/test/functional/com/mongodb/binding/SimpleSessionContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class SimpleSessionContext implements SessionContext {
3939

4040
@Override
4141
public boolean hasSession() {
42+
return false;
43+
}
44+
45+
@Override
46+
public boolean isImplicitSession() {
4247
return true;
4348
}
4449

driver-core/src/test/unit/com/mongodb/connection/TestSessionContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public boolean hasSession() {
3939
return false;
4040
}
4141

42+
@Override
43+
public boolean isImplicitSession() {
44+
throw new UnsupportedOperationException();
45+
}
46+
4247
@Override
4348
public BsonDocument getSessionId() {
4449
throw new UnsupportedOperationException();

driver-core/src/test/unit/com/mongodb/internal/session/ClientSessionContextSpecification.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class ClientSessionContextSpecification extends Specification {
3838
false;
3939
}
4040

41+
@Override
42+
boolean isImplicitSession() {
43+
throw new UnsupportedOperationException()
44+
}
45+
4146
@Override
4247
boolean notifyMessageSent() {
4348
throw new UnsupportedOperationException()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
384384
}
385385

386386
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
387-
def 'should not use a session for an unacknowledged write'() {
387+
def 'should not use an implicit session for an unacknowledged write'() {
388388
given:
389389
def commandListener = new TestCommandListener()
390390
def optionsBuilder = MongoClientOptions.builder()

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ private final class SyncClientSessionContext extends ClientSessionContext implem
139139
this.clientSession = clientSession;
140140
}
141141

142+
@Override
143+
public boolean isImplicitSession() {
144+
return ownsSession;
145+
}
142146

143147
@Override
144148
public boolean notifyMessageSent() {

0 commit comments

Comments
 (0)