Table of Contents

Method WithTransaction

Namespace
MongoDB.Driver
Assembly
MongoDB.Driver.dll

WithTransaction<TResult>(Func<IClientSessionHandle, CancellationToken, TResult>, TransactionOptions, CancellationToken)

Executes a callback within a transaction, with retries if needed.

TResult WithTransaction<TResult>(Func<IClientSessionHandle, CancellationToken, TResult> callback, TransactionOptions transactionOptions = null, CancellationToken cancellationToken = default)

Parameters

callback Func<IClientSessionHandle, CancellationToken, TResult>

The user defined callback.

transactionOptions TransactionOptions

The transaction options.

cancellationToken CancellationToken

The cancellation token.

Returns

TResult

The callback result.

Type Parameters

TResult

The type of callback result.

Remarks

If a command inside the callback fails, it may cause the transaction on the server to be aborted. This situation is normally handled transparently by the driver. However, if the application does not return that error from the callback, the driver will not be able to determine whether the transaction was aborted or not. The driver will then retry the callback indefinitely. To avoid this situation, the application MUST NOT silently handle errors within the callback. If the application needs to handle errors within the callback, it MUST return them after doing so.