|
1 |
| -/* Copyright 2018-present MongoDB Inc. |
| 1 | +/* Copyright 2010-present MongoDB Inc. |
2 | 2 | *
|
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | * you may not use this file except in compliance with the License.
|
@@ -29,7 +29,7 @@ namespace MongoDB.Driver.Core.Bindings
|
29 | 29 | /// Represents a session.
|
30 | 30 | /// </summary>
|
31 | 31 | /// <seealso cref="MongoDB.Driver.Core.Bindings.ICoreSession" />
|
32 |
| - public sealed class CoreSession : ICoreSession |
| 32 | + public sealed class CoreSession : ICoreSession, ICoreSessionInternal |
33 | 33 | {
|
34 | 34 | // private fields
|
35 | 35 | #pragma warning disable CA2213 // Disposable fields should be disposed
|
@@ -141,12 +141,15 @@ public bool IsInTransaction
|
141 | 141 |
|
142 | 142 | // public methods
|
143 | 143 | /// <inheritdoc />
|
144 |
| - public void AbortTransaction(CancellationToken cancellationToken = default(CancellationToken)) |
| 144 | + public void AbortTransaction(CancellationToken cancellationToken = default) |
| 145 | + => ((ICoreSessionInternal)this).AbortTransaction(null, cancellationToken); |
| 146 | + |
| 147 | + // TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter. |
| 148 | + void ICoreSessionInternal.AbortTransaction(AbortTransactionOptions options, CancellationToken cancellationToken) |
145 | 149 | {
|
146 | 150 | EnsureAbortTransactionCanBeCalled(nameof(AbortTransaction));
|
147 | 151 |
|
148 |
| - // TODO: CSOT implement proper way to obtain the operationContext |
149 |
| - var operationContext = new OperationContext(null, cancellationToken); |
| 152 | + using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken); |
150 | 153 | try
|
151 | 154 | {
|
152 | 155 | if (_currentTransaction.IsEmpty)
|
@@ -192,12 +195,15 @@ public bool IsInTransaction
|
192 | 195 | }
|
193 | 196 |
|
194 | 197 | /// <inheritdoc />
|
195 |
| - public async Task AbortTransactionAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| 198 | + public Task AbortTransactionAsync(CancellationToken cancellationToken = default) |
| 199 | + => ((ICoreSessionInternal)this).AbortTransactionAsync(null, cancellationToken); |
| 200 | + |
| 201 | + // TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter. |
| 202 | + async Task ICoreSessionInternal.AbortTransactionAsync(AbortTransactionOptions options, CancellationToken cancellationToken) |
196 | 203 | {
|
197 | 204 | EnsureAbortTransactionCanBeCalled(nameof(AbortTransaction));
|
198 | 205 |
|
199 |
| - // TODO: CSOT implement proper way to obtain the operationContext |
200 |
| - var operationContext = new OperationContext(null, cancellationToken); |
| 206 | + using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken); |
201 | 207 | try
|
202 | 208 | {
|
203 | 209 | if (_currentTransaction.IsEmpty)
|
@@ -292,12 +298,15 @@ public long AdvanceTransactionNumber()
|
292 | 298 | }
|
293 | 299 |
|
294 | 300 | /// <inheritdoc />
|
295 |
| - public void CommitTransaction(CancellationToken cancellationToken = default(CancellationToken)) |
| 301 | + public void CommitTransaction(CancellationToken cancellationToken = default) |
| 302 | + => ((ICoreSessionInternal)this).CommitTransaction(null, cancellationToken); |
| 303 | + |
| 304 | + // TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter. |
| 305 | + void ICoreSessionInternal.CommitTransaction(CommitTransactionOptions options, CancellationToken cancellationToken) |
296 | 306 | {
|
297 | 307 | EnsureCommitTransactionCanBeCalled(nameof(CommitTransaction));
|
298 | 308 |
|
299 |
| - // TODO: CSOT implement proper way to obtain the operationContext |
300 |
| - var operationContext = new OperationContext(null, cancellationToken); |
| 309 | + using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken); |
301 | 310 | try
|
302 | 311 | {
|
303 | 312 | _isCommitTransactionInProgress = true;
|
@@ -329,12 +338,15 @@ public long AdvanceTransactionNumber()
|
329 | 338 | }
|
330 | 339 |
|
331 | 340 | /// <inheritdoc />
|
332 |
| - public async Task CommitTransactionAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| 341 | + public Task CommitTransactionAsync(CancellationToken cancellationToken = default) |
| 342 | + => ((ICoreSessionInternal)this).CommitTransactionAsync(null, cancellationToken); |
| 343 | + |
| 344 | + // TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter. |
| 345 | + async Task ICoreSessionInternal.CommitTransactionAsync(CommitTransactionOptions options, CancellationToken cancellationToken) |
333 | 346 | {
|
334 | 347 | EnsureCommitTransactionCanBeCalled(nameof(CommitTransaction));
|
335 | 348 |
|
336 |
| - // TODO: CSOT implement proper way to obtain the operationContext |
337 |
| - var operationContext = new OperationContext(null, cancellationToken); |
| 349 | + using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken); |
338 | 350 | try
|
339 | 351 | {
|
340 | 352 | _isCommitTransactionInProgress = true;
|
@@ -563,6 +575,9 @@ private async Task<TResult> ExecuteEndTransactionOnPrimaryAsync<TResult>(Operati
|
563 | 575 | }
|
564 | 576 | }
|
565 | 577 |
|
| 578 | + private TimeSpan? GetTimeout(TimeSpan? timeout) |
| 579 | + => timeout ?? _options.DefaultTransactionOptions?.Timeout; |
| 580 | + |
566 | 581 | private TransactionOptions GetEffectiveTransactionOptions(TransactionOptions transactionOptions)
|
567 | 582 | {
|
568 | 583 | var readConcern = transactionOptions?.ReadConcern ?? _options.DefaultTransactionOptions?.ReadConcern ?? ReadConcern.Default;
|
|
0 commit comments