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 1 commit
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
34 changes: 34 additions & 0 deletions src/MongoDB.Driver/AbortTransactionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* 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()
{}
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for a new line for this?

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 suppose we do not need the parameter less constructor at all here. Thank you for bringing attention to this.


public AbortTransactionOptions(TimeSpan? timeout)
{
Timeout = Ensure.IsNullOrValidTimeout(timeout, nameof(timeout));
}

public TimeSpan? Timeout { get; }
}
}
42 changes: 25 additions & 17 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 @@ -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
internal void AbortTransaction(AbortTransactionOptions options, CancellationToken cancellationToken = default)
=> _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
internal Task AbortTransactionAsync(AbortTransactionOptions options, CancellationToken cancellationToken = default)
=> _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
internal void CommitTransaction(CommitTransactionOptions options, CancellationToken cancellationToken = default)
=> _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
internal Task CommitTransactionAsync(CommitTransactionOptions options, CancellationToken cancellationToken = default)
=> _coreSession.CommitTransactionAsync(options, cancellationToken);

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

Choose a reason for hiding this comment

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

same no need for a new line?

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 suppose we do not need the parameter less constructor at all here. Thank you for bringing attention to this.


public CommitTransactionOptions(TimeSpan? timeout)
{
Timeout = Ensure.IsNullOrValidTimeout(timeout, nameof(timeout));
}

public TimeSpan? Timeout { get; }
}

}

41 changes: 28 additions & 13 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 @@ -141,12 +141,15 @@ public bool IsInTransaction

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

// TODO: CSOT: Make it public when CSOT will be ready for GA
internal void AbortTransaction(AbortTransactionOptions options, CancellationToken cancellationToken = default)
{
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)
=> AbortTransactionAsync(null, cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA
internal async Task AbortTransactionAsync(AbortTransactionOptions options, CancellationToken cancellationToken = default)
{
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)
=> CommitTransaction(null, cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA
internal void CommitTransaction(CommitTransactionOptions options, CancellationToken cancellationToken = default)
{
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)
=> CommitTransactionAsync(null, cancellationToken);

// TODO: CSOT: Make it public when CSOT will be ready for GA
internal async Task CommitTransactionAsync(CommitTransactionOptions options, CancellationToken cancellationToken = default(CancellationToken))
Copy link
Contributor

Choose a reason for hiding this comment

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

Inconsistent default parameter syntax for cancellationToken for both methods. Standardize on either default or default(CancellationToken)

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.

{
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
137 changes: 137 additions & 0 deletions src/MongoDB.Driver/Core/Bindings/ICoreSessionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* 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 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: CSOT: Merge this extension methods in ICoreSession interface on major release
/// <summary>
/// Aborts the transaction.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="options">Abort transaction options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
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;
}

if (session is CoreSession coreSession)
{
coreSession.AbortTransaction(options, cancellationToken);
return;
}

if (session is WrappingCoreSession wrappingCoreSession)
{
wrappingCoreSession.AbortTransaction(options, cancellationToken);
return;
}

throw new InvalidOperationException("Cannot apply options on non CoreSession.");
}

/// <summary>
/// Aborts the transaction.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="options">Abort transaction options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
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);
}

if (session is CoreSession coreSession)
{
return coreSession.AbortTransactionAsync(options, cancellationToken);
}

if (session is WrappingCoreSession wrappingCoreSession)
{
return wrappingCoreSession.AbortTransactionAsync(options, cancellationToken);
}

throw new InvalidOperationException("Cannot apply options on non CoreSession.");
}

/// <summary>
/// Commits the transaction.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="options">Commit transaction options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
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;
}

if (session is CoreSession coreSession)
{
coreSession.CommitTransaction(options, cancellationToken);
return;
}

if (session is WrappingCoreSession wrappingCoreSession)
{
wrappingCoreSession.CommitTransaction(options, cancellationToken);
return;
}

throw new InvalidOperationException("Cannot apply options on non CoreSession.");
}

/// <summary>
/// Commits the transaction.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="options">Commit transaction options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
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);
}

if (session is CoreSession coreSession)
{
return coreSession.CommitTransactionAsync(options, cancellationToken);
}

if (session is WrappingCoreSession wrappingCoreSession)
{
return wrappingCoreSession.CommitTransactionAsync(options, cancellationToken);
}

throw new InvalidOperationException("Cannot apply options on non CoreSession.");
}
}
}

Loading