Skip to content

CSHARP-3552: CSOT: Transactions #1745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/MongoDB.Driver/AbortTransactionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright 2010-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.
*/

using System;
using MongoDB.Driver.Core.Misc;

namespace MongoDB.Driver
{
// TODO: CSOT: Make it public when CSOT will be ready for GA
internal sealed class AbortTransactionOptions
{
public AbortTransactionOptions(TimeSpan? timeout)
{
Timeout = Ensure.IsNullOrValidTimeout(timeout, nameof(timeout));
}

public TimeSpan? Timeout { get; }
}
}
44 changes: 26 additions & 18 deletions src/MongoDB.Driver/ClientSessionHandle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2017-present MongoDB Inc.
/* Copyright 2010-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.
Expand Down Expand Up @@ -26,7 +26,7 @@ namespace MongoDB.Driver
/// A client session handle.
/// </summary>
/// <seealso cref="MongoDB.Driver.IClientSessionHandle" />
internal sealed class ClientSessionHandle : IClientSessionHandle
internal sealed class ClientSessionHandle : IClientSessionHandle, IClientSessionInternal
{
// private fields
private readonly IMongoClient _client;
Expand Down Expand Up @@ -94,16 +94,20 @@ public IServerSession ServerSession

// public methods
/// <inheritdoc />
public void AbortTransaction(CancellationToken cancellationToken = default(CancellationToken))
{
_coreSession.AbortTransaction(cancellationToken);
}
public void AbortTransaction(CancellationToken cancellationToken = default)
=> _coreSession.AbortTransaction(cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter.
void IClientSessionInternal.AbortTransaction(AbortTransactionOptions options, CancellationToken cancellationToken)
=> _coreSession.AbortTransaction(options, cancellationToken);

/// <inheritdoc />
public Task AbortTransactionAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return _coreSession.AbortTransactionAsync(cancellationToken);
}
public Task AbortTransactionAsync(CancellationToken cancellationToken = default)
=> _coreSession.AbortTransactionAsync(cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter.
Task IClientSessionInternal.AbortTransactionAsync(AbortTransactionOptions options, CancellationToken cancellationToken)
=> _coreSession.AbortTransactionAsync(options, cancellationToken);

/// <inheritdoc />
public void AdvanceClusterTime(BsonDocument newClusterTime)
Expand All @@ -118,16 +122,20 @@ public void AdvanceOperationTime(BsonTimestamp newOperationTime)
}

/// <inheritdoc />
public void CommitTransaction(CancellationToken cancellationToken = default(CancellationToken))
{
_coreSession.CommitTransaction(cancellationToken);
}
public void CommitTransaction(CancellationToken cancellationToken = default)
=> _coreSession.CommitTransaction(cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter.
void IClientSessionInternal.CommitTransaction(CommitTransactionOptions options, CancellationToken cancellationToken)
=> _coreSession.CommitTransaction(options, cancellationToken);

/// <inheritdoc />
public Task CommitTransactionAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return _coreSession.CommitTransactionAsync(cancellationToken);
}
public Task CommitTransactionAsync(CancellationToken cancellationToken = default)
=> _coreSession.CommitTransactionAsync(cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter.
Task IClientSessionInternal.CommitTransactionAsync(CommitTransactionOptions options, CancellationToken cancellationToken)
=> _coreSession.CommitTransactionAsync(options, cancellationToken);

/// <inheritdoc />
public void Dispose()
Expand Down
32 changes: 32 additions & 0 deletions src/MongoDB.Driver/CommitTransactionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Copyright 2010-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.
*/

using System;
using MongoDB.Driver.Core.Misc;

namespace MongoDB.Driver
{
// TODO: CSOT: Make it public when CSOT will be ready for GA
internal sealed class CommitTransactionOptions
{
public CommitTransactionOptions(TimeSpan? timeout)
{
Timeout = Ensure.IsNullOrValidTimeout(timeout, nameof(timeout));
}

public TimeSpan? Timeout { get; }
}
}

43 changes: 29 additions & 14 deletions src/MongoDB.Driver/Core/Bindings/CoreSession.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2018-present MongoDB Inc.
/* Copyright 2010-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.
Expand Down Expand Up @@ -29,7 +29,7 @@ namespace MongoDB.Driver.Core.Bindings
/// Represents a session.
/// </summary>
/// <seealso cref="MongoDB.Driver.Core.Bindings.ICoreSession" />
public sealed class CoreSession : ICoreSession
public sealed class CoreSession : ICoreSession, ICoreSessionInternal
{
// private fields
#pragma warning disable CA2213 // Disposable fields should be disposed
Expand Down Expand Up @@ -141,12 +141,15 @@ public bool IsInTransaction

// public methods
/// <inheritdoc />
public void AbortTransaction(CancellationToken cancellationToken = default(CancellationToken))
public void AbortTransaction(CancellationToken cancellationToken = default)
=> ((ICoreSessionInternal)this).AbortTransaction(null, cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter.
void ICoreSessionInternal.AbortTransaction(AbortTransactionOptions options, CancellationToken cancellationToken)
{
EnsureAbortTransactionCanBeCalled(nameof(AbortTransaction));

// TODO: CSOT implement proper way to obtain the operationContext
var operationContext = new OperationContext(null, cancellationToken);
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken);
try
{
if (_currentTransaction.IsEmpty)
Expand Down Expand Up @@ -192,12 +195,15 @@ public bool IsInTransaction
}

/// <inheritdoc />
public async Task AbortTransactionAsync(CancellationToken cancellationToken = default(CancellationToken))
public Task AbortTransactionAsync(CancellationToken cancellationToken = default)
=> ((ICoreSessionInternal)this).AbortTransactionAsync(null, cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter.
async Task ICoreSessionInternal.AbortTransactionAsync(AbortTransactionOptions options, CancellationToken cancellationToken)
{
EnsureAbortTransactionCanBeCalled(nameof(AbortTransaction));

// TODO: CSOT implement proper way to obtain the operationContext
var operationContext = new OperationContext(null, cancellationToken);
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken);
try
{
if (_currentTransaction.IsEmpty)
Expand Down Expand Up @@ -292,12 +298,15 @@ public long AdvanceTransactionNumber()
}

/// <inheritdoc />
public void CommitTransaction(CancellationToken cancellationToken = default(CancellationToken))
public void CommitTransaction(CancellationToken cancellationToken = default)
=> ((ICoreSessionInternal)this).CommitTransaction(null, cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter.
void ICoreSessionInternal.CommitTransaction(CommitTransactionOptions options, CancellationToken cancellationToken)
{
EnsureCommitTransactionCanBeCalled(nameof(CommitTransaction));

// TODO: CSOT implement proper way to obtain the operationContext
var operationContext = new OperationContext(null, cancellationToken);
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken);
try
{
_isCommitTransactionInProgress = true;
Expand Down Expand Up @@ -329,12 +338,15 @@ public long AdvanceTransactionNumber()
}

/// <inheritdoc />
public async Task CommitTransactionAsync(CancellationToken cancellationToken = default(CancellationToken))
public Task CommitTransactionAsync(CancellationToken cancellationToken = default)
=> ((ICoreSessionInternal)this).CommitTransactionAsync(null, cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA and add default value to cancellationToken parameter.
async Task ICoreSessionInternal.CommitTransactionAsync(CommitTransactionOptions options, CancellationToken cancellationToken)
{
EnsureCommitTransactionCanBeCalled(nameof(CommitTransaction));

// TODO: CSOT implement proper way to obtain the operationContext
var operationContext = new OperationContext(null, cancellationToken);
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken);
try
{
_isCommitTransactionInProgress = true;
Expand Down Expand Up @@ -563,6 +575,9 @@ private async Task<TResult> ExecuteEndTransactionOnPrimaryAsync<TResult>(Operati
}
}

private TimeSpan? GetTimeout(TimeSpan? timeout)
=> timeout ?? _options.DefaultTransactionOptions?.Timeout;

private TransactionOptions GetEffectiveTransactionOptions(TransactionOptions transactionOptions)
{
var readConcern = transactionOptions?.ReadConcern ?? _options.DefaultTransactionOptions?.ReadConcern ?? ReadConcern.Default;
Expand Down
4 changes: 3 additions & 1 deletion src/MongoDB.Driver/Core/Bindings/CoreTransaction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2018-present MongoDB Inc.
/* Copyright 2010-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.
Expand Down Expand Up @@ -56,6 +56,8 @@ public CoreTransaction(long transactionNumber, TransactionOptions transactionOpt
/// </value>
public bool IsEmpty => _isEmpty;

internal OperationContext OperationContext { get; set; }

/// <summary>
/// Gets the transaction state.
/// </summary>
Expand Down
67 changes: 67 additions & 0 deletions src/MongoDB.Driver/Core/Bindings/ICoreSessionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright 2010-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.
*/

using System.Threading;
using System.Threading.Tasks;

namespace MongoDB.Driver.Core.Bindings
{
// TODO: CSOT: Make it public when CSOT will be ready for GA
internal static class ICoreSessionExtensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend creating a new ICoreSessionInternal interface to hold the new methods we hope to eventually add to ICoreSession.

These extension methods are useful to make it "look" (to us) like the new methods are already part of ICoreSession.

These extension methods should cast session to ICoreSessionInternal instead of to an open ended set of concrete implementations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All classes that implement ICoreSession (directly or indirectly) should also now implement ICoreSessionInternal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you!

{
// TODO: Merge these extension methods in ICoreSession interface on major release
public static void AbortTransaction(this ICoreSession session, AbortTransactionOptions options, CancellationToken cancellationToken = default)
{
if (options == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout)
{
session.AbortTransaction(cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forwarding the new method to the old method doesn't seem necessary but it also doesn't do any real harm other than adding more lines of code that probably aren't needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've decided to have this forwarding, just in case there are any external implementation of ICoreSession, this forwarding might reduce risk of breaking client's code.

return;
}

((ICoreSessionInternal)session).AbortTransaction(options, cancellationToken);
}

public static Task AbortTransactionAsync(this ICoreSession session, AbortTransactionOptions options, CancellationToken cancellationToken = default)
{
if (options == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout)
{
return session.AbortTransactionAsync(cancellationToken);
}

return ((ICoreSessionInternal)session).AbortTransactionAsync(options, cancellationToken);
}

public static void CommitTransaction(this ICoreSession session, CommitTransactionOptions options, CancellationToken cancellationToken = default)
{
if (options == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout)
{
session.CommitTransaction(cancellationToken);
return;
}

((ICoreSessionInternal)session).CommitTransaction(options, cancellationToken);
}

public static Task CommitTransactionAsync(this ICoreSession session, CommitTransactionOptions options, CancellationToken cancellationToken = default)
{
if (options == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout)
{
return session.CommitTransactionAsync(cancellationToken);
}

return ((ICoreSessionInternal)session).CommitTransactionAsync(options, cancellationToken);
}
}
}
28 changes: 28 additions & 0 deletions src/MongoDB.Driver/Core/Bindings/ICoreSessionInternal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2010-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.
*/

using System.Threading;
using System.Threading.Tasks;

namespace MongoDB.Driver.Core.Bindings;

// TODO: Merge this interface into ICoreSession on major release
internal interface ICoreSessionInternal
{
void AbortTransaction(AbortTransactionOptions options, CancellationToken cancellationToken = default);
Task AbortTransactionAsync(AbortTransactionOptions options, CancellationToken cancellationToken = default);
void CommitTransaction(CommitTransactionOptions options, CancellationToken cancellationToken = default);
Task CommitTransactionAsync(CommitTransactionOptions options, CancellationToken cancellationToken = default);
}
Loading