Table of Contents

Method ExecuteTransactionConditionalAsync

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.MSSqlClient.dll

ExecuteTransactionConditionalAsync<T>(SqlConnection, Func<SqlTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>>, IsolationLevel, CancellationToken)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, its Commit value is false, or cancellationToken is canceled.

public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this SqlConnection connection, Func<SqlTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

conditionalAction Func<SqlTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>>

The asynchronous handler to execute while the transaction is pending. It receives the transaction and cancellationToken. Returning a Commit value of true signals to commit the transaction.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

Returns

ValueTask<(bool Commit, T Value)>

A task that represents the asynchronous operation; its result is the value returned by conditionalAction. Its Commit value is true if the transaction was committed.

Type Parameters

T

The type of the value returned by the action.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or conditionalAction is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync<T>(SqlConnection, Func<SqlTransaction, ValueTask<(bool Commit, T Value)>>, IsolationLevel, CancellationToken)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, its Commit value is false, or cancellationToken is canceled.

public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this SqlConnection connection, Func<SqlTransaction, ValueTask<(bool Commit, T Value)>> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

conditionalAction Func<SqlTransaction, ValueTask<(bool Commit, T Value)>>

The asynchronous handler to execute while the transaction is pending. Returning a Commit value of true signals to commit the transaction.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

Returns

ValueTask<(bool Commit, T Value)>

A task that represents the asynchronous operation; its result is the value returned by conditionalAction. Its Commit value is true if the transaction was committed.

Type Parameters

T

The type of the value returned by the action.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

conditionalAction is null.

ArgumentNullException

connection is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync(SqlConnection, Func<SqlTransaction, ValueTask<bool>>, IsolationLevel, CancellationToken)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, returns false, or cancellationToken is canceled.

public static ValueTask<bool> ExecuteTransactionConditionalAsync(this SqlConnection connection, Func<SqlTransaction, ValueTask<bool>> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

The asynchronous handler to execute while the transaction is pending. Returning true signals to commit the transaction.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

Returns

ValueTask<bool>

A task that represents the asynchronous operation; its result is true if the transaction was committed; otherwise, false.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or conditionalAction is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync<T>(SqlConnection, Func<SqlTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>>, CancellationToken)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, its Commit value is false, or cancellationToken is canceled.

public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this SqlConnection connection, Func<SqlTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>> conditionalAction, CancellationToken cancellationToken)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

conditionalAction Func<SqlTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>>

The asynchronous handler to execute while the transaction is pending. It receives the transaction and cancellationToken. Returning a Commit value of true signals to commit the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

Returns

ValueTask<(bool Commit, T Value)>

A task that represents the asynchronous operation; its result is the value returned by conditionalAction. Its Commit value is true if the transaction was committed.

Type Parameters

T

The type of the value returned by the action.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or conditionalAction is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync<T>(SqlConnection, Func<SqlTransaction, ValueTask<(bool Commit, T Value)>>, CancellationToken)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, its Commit value is false, or cancellationToken is canceled.

public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this SqlConnection connection, Func<SqlTransaction, ValueTask<(bool Commit, T Value)>> conditionalAction, CancellationToken cancellationToken)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

conditionalAction Func<SqlTransaction, ValueTask<(bool Commit, T Value)>>

The asynchronous handler to execute while the transaction is pending. Returning a Commit value of true signals to commit the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

Returns

ValueTask<(bool Commit, T Value)>

A task that represents the asynchronous operation; its result is the value returned by conditionalAction. Its Commit value is true if the transaction was committed.

Type Parameters

T

The type of the value returned by the action.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

conditionalAction is null.

ArgumentNullException

connection is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync(SqlConnection, Func<SqlTransaction, ValueTask<bool>>, CancellationToken)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, returns false, or cancellationToken is canceled.

public static ValueTask<bool> ExecuteTransactionConditionalAsync(this SqlConnection connection, Func<SqlTransaction, ValueTask<bool>> conditionalAction, CancellationToken cancellationToken)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

The asynchronous handler to execute while the transaction is pending. Returning true signals to commit the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

Returns

ValueTask<bool>

A task that represents the asynchronous operation; its result is true if the transaction was committed; otherwise, false.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or conditionalAction is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync<T>(SqlConnection, IsolationLevel, CancellationToken, Func<SqlTransaction, ValueTask<(bool Commit, T Value)>>)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, its Commit value is false, or cancellationToken is canceled.

public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this SqlConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<SqlTransaction, ValueTask<(bool Commit, T Value)>> conditionalAction)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

conditionalAction Func<SqlTransaction, ValueTask<(bool Commit, T Value)>>

The asynchronous handler to execute while the transaction is pending. Returning a Commit value of true signals to commit the transaction.

Returns

ValueTask<(bool Commit, T Value)>

A task that represents the asynchronous operation; its result is the value returned by conditionalAction. Its Commit value is true if the transaction was committed.

Type Parameters

T

The type of the value returned by the action.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

conditionalAction is null.

ArgumentNullException

connection is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync(SqlConnection, IsolationLevel, CancellationToken, Func<SqlTransaction, ValueTask<bool>>)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, returns false, or cancellationToken is canceled.

public static ValueTask<bool> ExecuteTransactionConditionalAsync(this SqlConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<SqlTransaction, ValueTask<bool>> conditionalAction)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

The asynchronous handler to execute while the transaction is pending. Returning true signals to commit the transaction.

Returns

ValueTask<bool>

A task that represents the asynchronous operation; its result is true if the transaction was committed; otherwise, false.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or conditionalAction is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync<T>(SqlConnection, IsolationLevel, Func<SqlTransaction, ValueTask<(bool Commit, T Value)>>, CancellationToken)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, its Commit value is false, or cancellationToken is canceled.

public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this SqlConnection connection, IsolationLevel isolationLevel, Func<SqlTransaction, ValueTask<(bool Commit, T Value)>> conditionalAction, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

isolationLevel IsolationLevel

The isolation level for the transaction.

conditionalAction Func<SqlTransaction, ValueTask<(bool Commit, T Value)>>

The asynchronous handler to execute while the transaction is pending. Returning a Commit value of true signals to commit the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

Returns

ValueTask<(bool Commit, T Value)>

A task that represents the asynchronous operation; its result is the value returned by conditionalAction. Its Commit value is true if the transaction was committed.

Type Parameters

T

The type of the value returned by the action.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

conditionalAction is null.

ArgumentNullException

connection is null. The exception is reported through the returned task.

ExecuteTransactionConditionalAsync(SqlConnection, IsolationLevel, Func<SqlTransaction, ValueTask<bool>>, CancellationToken)

Asynchronously runs the action in a transaction on the connection, committing it unless the action throws, returns false, or cancellationToken is canceled.

public static ValueTask<bool> ExecuteTransactionConditionalAsync(this SqlConnection connection, IsolationLevel isolationLevel, Func<SqlTransaction, ValueTask<bool>> conditionalAction, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

isolationLevel IsolationLevel

The isolation level for the transaction.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

The asynchronous handler to execute while the transaction is pending. Returning true signals to commit the transaction.

cancellationToken CancellationToken

A token to observe while the operation runs. If it is canceled before the transaction is committed, the transaction is rolled back, or is never begun.

Returns

ValueTask<bool>

A task that represents the asynchronous operation; its result is true if the transaction was committed; otherwise, false.

Remarks

If the connection is not open, it is opened first and closed (not disposed) when the transaction completes, even if it fails. A connection that was broken is closed and opened again, so it is closed afterwards too. A connection that was already open is left open. A failure while closing is ignored, as by then the transaction has already been committed or rolled back. To keep a connection open across several operations, open it before calling.

An exception thrown by the action, or a cancellation, rolls the transaction back and propagates. The transaction is disposed before the connection is closed. The action should not commit or roll back the transaction itself; completion is handled here. When no isolation level is given, Unspecified is used.

The transaction is disposed with DisposeAsync, committed with CommitAsync, and the connection closed with CloseAsync, except on .NET Framework (net472), which does all three synchronously. The token is checked just before the commit; the commit is not canceled once it has begun.

Exceptions

OperationCanceledException

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or conditionalAction is null. The exception is reported through the returned task.