ClientSessionHandle.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright 2017-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using MongoDB.Bson;
  18. using MongoDB.Driver.Core.Bindings;
  19. namespace MongoDB.Driver
  20. {
  21. /// <summary>
  22. /// A client session handle.
  23. /// </summary>
  24. /// <seealso cref="MongoDB.Driver.IClientSessionHandle" />
  25. internal sealed class ClientSessionHandle : IClientSessionHandle
  26. {
  27. // private fields
  28. private readonly IMongoClient _client;
  29. private readonly ICoreSessionHandle _coreSession;
  30. private bool _disposed;
  31. private readonly ClientSessionOptions _options;
  32. private readonly IServerSession _serverSession;
  33. // constructors
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="ClientSessionHandle" /> class.
  36. /// </summary>
  37. /// <param name="client">The client.</param>
  38. /// <param name="options">The options.</param>
  39. /// <param name="coreSession">The wrapped session.</param>
  40. public ClientSessionHandle(IMongoClient client, ClientSessionOptions options, ICoreSessionHandle coreSession)
  41. {
  42. _client = client;
  43. _options = options;
  44. _coreSession = coreSession;
  45. _serverSession = new ServerSession(coreSession.ServerSession);
  46. }
  47. // public properties
  48. /// <inheritdoc />
  49. public IMongoClient Client => _client;
  50. /// <inheritdoc />
  51. public BsonDocument ClusterTime => _coreSession.ClusterTime;
  52. /// <inheritdoc />
  53. public bool IsImplicit => _coreSession.IsImplicit;
  54. /// <inheritdoc />
  55. public bool IsInTransaction => _coreSession.IsInTransaction;
  56. /// <inheritdoc />
  57. public BsonTimestamp OperationTime => _coreSession.OperationTime;
  58. /// <inheritdoc />
  59. public ClientSessionOptions Options => _options;
  60. /// <inheritdoc />
  61. public IServerSession ServerSession => _serverSession;
  62. /// <inheritdoc />
  63. public ICoreSessionHandle WrappedCoreSession => _coreSession;
  64. // public methods
  65. /// <inheritdoc />
  66. public void AbortTransaction(CancellationToken cancellationToken = default(CancellationToken))
  67. {
  68. _coreSession.AbortTransaction(cancellationToken);
  69. }
  70. /// <inheritdoc />
  71. public Task AbortTransactionAsync(CancellationToken cancellationToken = default(CancellationToken))
  72. {
  73. return _coreSession.AbortTransactionAsync(cancellationToken);
  74. }
  75. /// <inheritdoc />
  76. public void AdvanceClusterTime(BsonDocument newClusterTime)
  77. {
  78. _coreSession.AdvanceClusterTime(newClusterTime);
  79. }
  80. /// <inheritdoc />
  81. public void AdvanceOperationTime(BsonTimestamp newOperationTime)
  82. {
  83. _coreSession.AdvanceOperationTime(newOperationTime);
  84. }
  85. /// <inheritdoc />
  86. public void CommitTransaction(CancellationToken cancellationToken = default(CancellationToken))
  87. {
  88. _coreSession.CommitTransaction(cancellationToken);
  89. }
  90. /// <inheritdoc />
  91. public Task CommitTransactionAsync(CancellationToken cancellationToken = default(CancellationToken))
  92. {
  93. return _coreSession.CommitTransactionAsync(cancellationToken);
  94. }
  95. /// <inheritdoc />
  96. public void Dispose()
  97. {
  98. if (!_disposed)
  99. {
  100. _coreSession.Dispose();
  101. _serverSession.Dispose();
  102. _disposed = true;
  103. }
  104. }
  105. /// <inheritdoc />
  106. public IClientSessionHandle Fork()
  107. {
  108. return new ClientSessionHandle(_client, _options, _coreSession.Fork());
  109. }
  110. /// <inheritdoc />
  111. public void StartTransaction(TransactionOptions transactionOptions = null)
  112. {
  113. var effectiveTransactionOptions = GetEffectiveTransactionOptions(transactionOptions);
  114. _coreSession.StartTransaction(effectiveTransactionOptions);
  115. }
  116. // private methods
  117. private TransactionOptions GetEffectiveTransactionOptions(TransactionOptions transactionOptions)
  118. {
  119. var defaultTransactionOptions = _options?.DefaultTransactionOptions;
  120. var readConcern = transactionOptions?.ReadConcern ?? defaultTransactionOptions?.ReadConcern ?? _client.Settings?.ReadConcern ?? ReadConcern.Default;
  121. var readPreference = transactionOptions?.ReadPreference ?? defaultTransactionOptions?.ReadPreference ?? _client.Settings?.ReadPreference ?? ReadPreference.Primary;
  122. var writeConcern = transactionOptions?.WriteConcern ?? defaultTransactionOptions?.WriteConcern ?? _client.Settings?.WriteConcern ?? new WriteConcern();
  123. return new TransactionOptions(readConcern, readPreference, writeConcern);
  124. }
  125. }
  126. }