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)

Begins a transaction before executing the action. Commits if there are no exceptions, the 'Commit' value from the action is true and the optional cancellation token has not been cancelled. Otherwise rolls-back the transaction.

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 transact with.

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

The handler to execute while a 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 that if cancelled will cause this transaction to be aborted or rolled-back.

Returns

ValueTask<(bool Commit, T Value)>

The value returned from the conditional action.

Type Parameters

T

The value returned from the action.

Remarks

The connection does not need to be opened beforehand. If it is not open, it is opened first and then closed (not disposed) when the transaction completes, even if it fails. 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.

A transaction that is not committed is rolled back by disposing it, which is the standard ADO.NET pattern. The .NET documentation for Dispose() notes that this is provider specific. The transaction is always disposed before the connection is closed, as some providers only roll back on dispose while the connection is still open. The action should not commit or roll back the transaction itself; completion is handled here.

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

Begins a transaction before executing the action. Commits if there are no exceptions, the 'Commit' value from the action is true, and the optional provided token is not cancelled. Otherwise rolls-back the transaction.

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 transact with.

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

The handler to execute while a 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 that if cancelled will cause this transaction to be aborted or rolled-back.

Returns

ValueTask<(bool Commit, T Value)>

The value of the awaited action.

Type Parameters

T

The value returned from the action.

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

Begins a transaction before executing the action. Commits if there are no exceptions, the 'Commit' value from the action is true, and the optional provided token is not cancelled. Otherwise rolls-back the transaction.

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 transact with.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

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

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A token that if cancelled will cause this transaction to be aborted or rolled-back.

Returns

ValueTask<bool>

The value of the awaited action.

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

Begins a transaction before executing the action. Commits if there are no exceptions, the 'Commit' value from the action is true and the optional cancellation token has not been cancelled. Otherwise rolls-back the transaction.

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 transact with.

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

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

cancellationToken CancellationToken

A token that if cancelled will cause this transaction to be aborted or rolled-back.

Returns

ValueTask<(bool Commit, T Value)>

The value returned from the conditional action.

Type Parameters

T

The value returned from the action.

Remarks

The connection does not need to be opened beforehand. If it is not open, it is opened first and then closed (not disposed) when the transaction completes, even if it fails. 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.

A transaction that is not committed is rolled back by disposing it, which is the standard ADO.NET pattern. The .NET documentation for Dispose() notes that this is provider specific. The transaction is always disposed before the connection is closed, as some providers only roll back on dispose while the connection is still open. The action should not commit or roll back the transaction itself; completion is handled here.

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

Begins a transaction before executing the action. Commits if there are no exceptions, the 'Commit' value from the action is true, and the optional provided token is not cancelled. Otherwise rolls-back the transaction.

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 transact with.

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

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

cancellationToken CancellationToken

A token that if cancelled will cause this transaction to be aborted or rolled-back.

Returns

ValueTask<(bool Commit, T Value)>

The value of the awaited action.

Type Parameters

T

The value returned from the action.

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

Begins a transaction before executing the action. Commits if there are no exceptions, the 'Commit' value from the action is true, and the optional provided token is not cancelled. Otherwise rolls-back the transaction.

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

Parameters

connection SqlConnection

The connection to transact with.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

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

cancellationToken CancellationToken

A token that if cancelled will cause this transaction to be aborted or rolled-back.

Returns

ValueTask<bool>

The value of the awaited action.

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

Begins a transaction before executing the action. Commits if there are no exceptions and the 'Commit' value from the action is true. Otherwise rolls-back the transaction.

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 transact with.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A token that if cancelled will cause this transaction to be aborted or rolled-back.

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

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

Returns

ValueTask<(bool Commit, T Value)>

The value of the awaited action.

Type Parameters

T

The value returned from the action.

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

Begins a transaction before executing the action. Commits if there are no exceptions and the value from the action is true. Otherwise rolls-back the transaction.

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

Parameters

connection SqlConnection

The connection to transact with.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A token that if cancelled will cause this transaction to be aborted or rolled-back.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

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

Returns

ValueTask<bool>

The value of the awaited action.

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

Begins a transaction before executing the action. Commits if there are no exceptions and the 'Commit' value from the action is true. Otherwise rolls-back the transaction.

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 transact with.

isolationLevel IsolationLevel

The isolation level for the transaction.

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

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

cancellationToken CancellationToken

An optional token that if cancelled will cause this transaction to be aborted or rolled-back.

Returns

ValueTask<(bool Commit, T Value)>

The value of the awaited action.

Type Parameters

T

The value returned from the action.

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

Begins a transaction before executing the action. Commits if there are no exceptions and the value from the action is true. Otherwise rolls-back the transaction.

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

Parameters

connection SqlConnection

The connection to transact with.

isolationLevel IsolationLevel

The isolation level for the transaction.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

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

cancellationToken CancellationToken

An optional token that if cancelled will cause this transaction to be aborted or rolled-back.

Returns

ValueTask<bool>

The value of the awaited action.

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

Begins a transaction before executing the action. Commits if there are no exceptions, the 'Commit' value from the action is true, and the optional provided token is not cancelled. Otherwise rolls-back the transaction.

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

Parameters

connection SqlConnection

The connection to transact with.

cancellationToken CancellationToken

A token that if cancelled will cause this transaction to be aborted or rolled-back.

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

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

Returns

ValueTask<(bool Commit, T Value)>

The value of the awaited action.

Type Parameters

T

The value returned from the action.

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

Begins a transaction before executing the action. Commits if there are no exceptions, the value from the action is true, and the optional provided token is not cancelled. Otherwise rolls-back the transaction.

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

Parameters

connection SqlConnection

The connection to transact with.

cancellationToken CancellationToken

A token that if cancelled will cause this transaction to be aborted or rolled-back.

conditionalAction Func<SqlTransaction, ValueTask<bool>>

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

Returns

ValueTask<bool>

The value of the awaited action.