Skip to content

Commit aaa2115

Browse files
committed
CSHARP-3927: Drivers should check out an implicit session only after checking out a connection
1 parent 0ce5205 commit aaa2115

File tree

11 files changed

+117
-19
lines changed

11 files changed

+117
-19
lines changed

src/MongoDB.Driver.Core/Core/Bindings/CoreSession.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public sealed class CoreSession : ICoreSession
3939
private bool _isCommitTransactionInProgress;
4040
private readonly IOperationClock _operationClock = new OperationClock();
4141
private readonly CoreSessionOptions _options;
42-
private readonly ICoreServerSession _serverSession;
42+
private readonly Lazy<ICoreServerSession> _serverSession;
4343
private BsonTimestamp _snapshotTime;
4444

4545
// constructors
@@ -49,13 +49,32 @@ public sealed class CoreSession : ICoreSession
4949
/// <param name="cluster">The cluster.</param>
5050
/// <param name="serverSession">The server session.</param>
5151
/// <param name="options">The options.</param>
52+
[Obsolete("This constructor is deprecated. Avoid using CoreSession directly.")]
5253
public CoreSession(
5354
ICluster cluster,
5455
ICoreServerSession serverSession,
5556
CoreSessionOptions options)
57+
: this(cluster, options: options)
58+
{
59+
Ensure.IsNotNull(serverSession, nameof(serverSession));
60+
_serverSession = new Lazy<ICoreServerSession>(() => serverSession);
61+
}
62+
63+
internal CoreSession(
64+
ICluster cluster,
65+
ICoreServerSessionPool serverSessionPool,
66+
CoreSessionOptions options)
67+
: this(cluster, options)
68+
{
69+
Ensure.IsNotNull(serverSessionPool, nameof(serverSessionPool));
70+
_serverSession = new Lazy<ICoreServerSession>(() => serverSessionPool.AcquireSession());
71+
}
72+
73+
private CoreSession(
74+
ICluster cluster,
75+
CoreSessionOptions options)
5676
{
5777
_cluster = Ensure.IsNotNull(cluster, nameof(cluster));
58-
_serverSession = Ensure.IsNotNull(serverSession, nameof(serverSession));
5978
_options = Ensure.IsNotNull(options, nameof(options));
6079
}
6180

@@ -75,13 +94,13 @@ public CoreSession(
7594
public CoreTransaction CurrentTransaction => _currentTransaction;
7695

7796
/// <inheritdoc />
78-
public BsonDocument Id => _serverSession.Id;
97+
public BsonDocument Id => _serverSession.Value.Id;
7998

8099
/// <inheritdoc />
81100
public bool IsCausallyConsistent => _options.IsCausallyConsistent;
82101

83102
/// <inheritdoc />
84-
public bool IsDirty => _serverSession.IsDirty;
103+
public bool IsDirty => _serverSession.Value.IsDirty;
85104

86105
/// <inheritdoc />
87106
public bool IsImplicit => _options.IsImplicit;
@@ -120,7 +139,7 @@ public bool IsInTransaction
120139
public CoreSessionOptions Options => _options;
121140

122141
/// <inheritdoc />
123-
public ICoreServerSession ServerSession => _serverSession;
142+
public ICoreServerSession ServerSession => _serverSession.Value;
124143

125144
/// <inheritdoc />
126145
public BsonTimestamp SnapshotTime => _snapshotTime;
@@ -270,7 +289,7 @@ public void AdvanceOperationTime(BsonTimestamp newOperationTime)
270289
/// <inheritdoc />
271290
public long AdvanceTransactionNumber()
272291
{
273-
return _serverSession.AdvanceTransactionNumber();
292+
return _serverSession.Value.AdvanceTransactionNumber();
274293
}
275294

276295
/// <inheritdoc />
@@ -367,15 +386,15 @@ public void Dispose()
367386
}
368387

369388
_currentTransaction?.UnpinAll();
370-
_serverSession.Dispose();
389+
_serverSession.Value.Dispose();
371390
_disposed = true;
372391
}
373392
}
374393

375394
/// <inheritdoc />
376395
public void MarkDirty()
377396
{
378-
_serverSession.MarkDirty();
397+
_serverSession.Value.MarkDirty();
379398
}
380399

381400
/// <inheritdoc />
@@ -406,7 +425,7 @@ public void SetSnapshotTimeIfNeeded(BsonTimestamp snapshotTime)
406425
/// <inheritdoc />
407426
public void WasUsed()
408427
{
409-
_serverSession.WasUsed();
428+
_serverSession.Value.WasUsed();
410429
}
411430

412431
// private methods

src/MongoDB.Driver.Core/Core/Clusters/Cluster.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,7 @@ public async Task<IServer> SelectServerAsync(IServerSelector selector, Cancellat
329329
public ICoreSessionHandle StartSession(CoreSessionOptions options)
330330
{
331331
options = options ?? new CoreSessionOptions();
332-
var serverSession = AcquireServerSession();
333-
var session = new CoreSession(this, serverSession, options);
332+
var session = new CoreSession(this, _serverSessionPool, options);
334333
return new CoreSessionHandle(session);
335334
}
336335

src/MongoDB.Driver.Core/Core/Clusters/LoadBalancedCluster.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ public ICoreSessionHandle StartSession(CoreSessionOptions options = null)
230230
ThrowIfDisposed();
231231

232232
options = options ?? new CoreSessionOptions();
233-
var serverSession = AcquireServerSession();
234-
var session = new CoreSession(this, serverSession, options);
233+
var session = new CoreSession(this, _serverSessionPool, options);
235234
return new CoreSessionHandle(session);
236235
}
237236

src/MongoDB.Driver/ClientSessionHandle.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using MongoDB.Bson;
2020
using MongoDB.Driver.Core.Bindings;
2121
using MongoDB.Driver.Core.Misc;
22-
using MongoDB.Driver.Support;
2322

2423
namespace MongoDB.Driver
2524
{
@@ -35,7 +34,7 @@ internal sealed class ClientSessionHandle : IClientSessionHandle
3534
private readonly ICoreSessionHandle _coreSession;
3635
private bool _disposed;
3736
private readonly ClientSessionOptions _options;
38-
private readonly IServerSession _serverSession;
37+
private IServerSession _serverSession;
3938

4039
// constructors
4140
/// <summary>
@@ -54,7 +53,6 @@ internal ClientSessionHandle(IMongoClient client, ClientSessionOptions options,
5453
_client = client;
5554
_options = options;
5655
_coreSession = coreSession;
57-
_serverSession = new ServerSession(coreSession.ServerSession);
5856
_clock = clock;
5957
}
6058

@@ -78,7 +76,18 @@ internal ClientSessionHandle(IMongoClient client, ClientSessionOptions options,
7876
public ClientSessionOptions Options => _options;
7977

8078
/// <inheritdoc />
81-
public IServerSession ServerSession => _serverSession;
79+
public IServerSession ServerSession
80+
{
81+
get
82+
{
83+
if (_serverSession == null)
84+
{
85+
_serverSession = new ServerSession(_coreSession.ServerSession);
86+
}
87+
88+
return _serverSession;
89+
}
90+
}
8291

8392
/// <inheritdoc />
8493
public ICoreSessionHandle WrappedCoreSession => _coreSession;
@@ -126,7 +135,7 @@ public void Dispose()
126135
if (!_disposed)
127136
{
128137
_coreSession.Dispose();
129-
_serverSession.Dispose();
138+
_serverSession?.Dispose();
130139
_disposed = true;
131140
}
132141
}

tests/MongoDB.Driver.Core.Tests/Core/Bindings/CoreSessionTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public void constructor_should_initialize_instance()
4040
var serverSession = Mock.Of<ICoreServerSession>();
4141
var options = new CoreSessionOptions();
4242

43+
#pragma warning disable CS0618 // Type or member is obsolete
4344
var result = new CoreSession(cluster, serverSession, options);
45+
#pragma warning restore CS0618 // Type or member is obsolete
4446

4547
result.Cluster.Should().BeSameAs(cluster);
4648
result.CurrentTransaction.Should().BeNull();
@@ -440,7 +442,9 @@ private CoreSession CreateSubject(
440442
cluster = cluster ?? CreateMockReplicaSetCluster();
441443
serverSession = serverSession ?? Mock.Of<ICoreServerSession>();
442444
options = options ?? new CoreSessionOptions();
445+
#pragma warning disable CS0618 // Type or member is obsolete
443446
return new CoreSession(cluster, serverSession, options);
447+
#pragma warning restore CS0618 // Type or member is obsolete
444448
}
445449

446450
private CoreSession CreateSubject(ClusterDescription clusterDescription)

tests/MongoDB.Driver.Tests/ClientSessionHandleTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ public void Fork_should_return_expected_result()
252252
var cluster = Mock.Of<ICluster>();
253253
var coreServerSession = new CoreServerSession();
254254
var options = new ClientSessionOptions();
255+
#pragma warning disable CS0618 // Type or member is obsolete
255256
var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore());
257+
#pragma warning restore CS0618 // Type or member is obsolete
256258
var coreSessionHandle = new CoreSessionHandle(coreSession);
257259
var subject = CreateSubject(coreSession: coreSessionHandle);
258260
coreSessionHandle.ReferenceCount().Should().Be(1);

tests/MongoDB.Driver.Tests/MockOperationExecutor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ public IClientSessionHandle StartImplicitSession(CancellationToken cancellationT
181181
var cluster = Mock.Of<ICluster>();
182182
var options = new ClientSessionOptions();
183183
var coreServerSession = new CoreServerSession();
184+
#pragma warning disable CS0618 // Type or member is obsolete
184185
var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore(isImplicit: true));
186+
#pragma warning restore CS0618 // Type or member is obsolete
185187
var coreSessionHandle = new CoreSessionHandle(coreSession);
186188
return new ClientSessionHandle(_client, options, coreSessionHandle);
187189
}

tests/MongoDB.Driver.Tests/MongoClientTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ private IClientSessionHandle CreateClientSession()
425425
var options = new ClientSessionOptions();
426426
var cluster = Mock.Of<ICluster>();
427427
var coreServerSession = new CoreServerSession();
428+
#pragma warning disable CS0618 // Type or member is obsolete
428429
var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore());
430+
#pragma warning restore CS0618 // Type or member is obsolete
429431
var coreSessionHandle = new CoreSessionHandle(coreSession);
430432
return new ClientSessionHandle(client, options, coreSessionHandle);
431433
}

tests/MongoDB.Driver.Tests/MongoCollectionImplTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,7 +3755,9 @@ private IClientSessionHandle CreateSession(bool usingSession)
37553755
var cluster = Mock.Of<ICluster>();
37563756
var options = new ClientSessionOptions();
37573757
var coreServerSession = new CoreServerSession();
3758+
#pragma warning disable CS0618 // Type or member is obsolete
37583759
var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore());
3760+
#pragma warning restore CS0618 // Type or member is obsolete
37593761
var coreSessionHandle = new CoreSessionHandle(coreSession);
37603762
return new ClientSessionHandle(client, options, coreSessionHandle);
37613763
}

tests/MongoDB.Driver.Tests/MongoDatabaseImplTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,9 @@ private IClientSessionHandle CreateSession(bool usingSession)
14521452
var cluster = Mock.Of<ICluster>();
14531453
var options = new ClientSessionOptions();
14541454
var coreServerSession = new CoreServerSession();
1455+
#pragma warning disable CS0618 // Type or member is obsolete
14551456
var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore());
1457+
#pragma warning restore CS0618 // Type or member is obsolete
14561458
var coreSessionHandle = new CoreSessionHandle(coreSession);
14571459
return new ClientSessionHandle(_client, options, coreSessionHandle);
14581460
}

0 commit comments

Comments
 (0)