Method ExecuteTransactionAsync
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.MSSqlClient.dll
ExecuteTransactionAsync<T>(SqlConnection, Func<SqlTransaction, ValueTask<T>>, IsolationLevel, CancellationToken)
Asynchronously runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.
public static ValueTask<T> ExecuteTransactionAsync<T>(this SqlConnection connection, Func<SqlTransaction, ValueTask<T>> action, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
actionFunc<SqlTransaction, ValueTask<T>>The asynchronous handler to execute while the transaction is pending.
isolationLevelIsolationLevelThe isolation level for the transaction.
cancellationTokenCancellationTokenA 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<T>
A task that represents the asynchronous operation; its result is the value returned by
action.
Type Parameters
TThe 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
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null. The exception is reported through the returned task.
ExecuteTransactionAsync(SqlConnection, Func<SqlTransaction, ValueTask>, IsolationLevel, CancellationToken)
Asynchronously runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.
public static ValueTask ExecuteTransactionAsync(this SqlConnection connection, Func<SqlTransaction, ValueTask> action, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
actionFunc<SqlTransaction, ValueTask>The asynchronous handler to execute while the transaction is pending.
isolationLevelIsolationLevelThe isolation level for the transaction.
cancellationTokenCancellationTokenA 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
A task that represents the asynchronous operation.
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
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null. The exception is reported through the returned task.
ExecuteTransactionAsync<T>(SqlConnection, Func<SqlTransaction, ValueTask<T>>, CancellationToken)
Asynchronously runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.
public static ValueTask<T> ExecuteTransactionAsync<T>(this SqlConnection connection, Func<SqlTransaction, ValueTask<T>> action, CancellationToken cancellationToken)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
actionFunc<SqlTransaction, ValueTask<T>>The asynchronous handler to execute while the transaction is pending.
cancellationTokenCancellationTokenA 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<T>
A task that represents the asynchronous operation; its result is the value returned by
action.
Type Parameters
TThe 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
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null. The exception is reported through the returned task.
ExecuteTransactionAsync(SqlConnection, Func<SqlTransaction, ValueTask>, CancellationToken)
Asynchronously runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.
public static ValueTask ExecuteTransactionAsync(this SqlConnection connection, Func<SqlTransaction, ValueTask> action, CancellationToken cancellationToken)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
actionFunc<SqlTransaction, ValueTask>The asynchronous handler to execute while the transaction is pending.
cancellationTokenCancellationTokenA 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
A task that represents the asynchronous operation.
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
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null. The exception is reported through the returned task.
ExecuteTransactionAsync<T>(SqlConnection, IsolationLevel, CancellationToken, Func<SqlTransaction, ValueTask<T>>)
Asynchronously runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.
public static ValueTask<T> ExecuteTransactionAsync<T>(this SqlConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<SqlTransaction, ValueTask<T>> action)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
cancellationTokenCancellationTokenA 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.
actionFunc<SqlTransaction, ValueTask<T>>The asynchronous handler to execute while the transaction is pending.
Returns
- ValueTask<T>
A task that represents the asynchronous operation; its result is the value returned by
action.
Type Parameters
TThe 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
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null. The exception is reported through the returned task.
ExecuteTransactionAsync(SqlConnection, IsolationLevel, CancellationToken, Func<SqlTransaction, ValueTask>)
Asynchronously runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.
public static ValueTask ExecuteTransactionAsync(this SqlConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<SqlTransaction, ValueTask> action)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
cancellationTokenCancellationTokenA 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.
actionFunc<SqlTransaction, ValueTask>The asynchronous handler to execute while the transaction is pending.
Returns
- ValueTask
A task that represents the asynchronous operation.
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
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null. The exception is reported through the returned task.
ExecuteTransactionAsync<T>(SqlConnection, IsolationLevel, Func<SqlTransaction, ValueTask<T>>, CancellationToken)
Asynchronously runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.
public static ValueTask<T> ExecuteTransactionAsync<T>(this SqlConnection connection, IsolationLevel isolationLevel, Func<SqlTransaction, ValueTask<T>> action, CancellationToken cancellationToken = default)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
actionFunc<SqlTransaction, ValueTask<T>>The asynchronous handler to execute while the transaction is pending.
cancellationTokenCancellationTokenA 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<T>
A task that represents the asynchronous operation; its result is the value returned by
action.
Type Parameters
TThe 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
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null. The exception is reported through the returned task.
ExecuteTransactionAsync(SqlConnection, IsolationLevel, Func<SqlTransaction, ValueTask>, CancellationToken)
Asynchronously runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.
public static ValueTask ExecuteTransactionAsync(this SqlConnection connection, IsolationLevel isolationLevel, Func<SqlTransaction, ValueTask> action, CancellationToken cancellationToken = default)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
actionFunc<SqlTransaction, ValueTask>The asynchronous handler to execute while the transaction is pending.
cancellationTokenCancellationTokenA 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
A task that represents the asynchronous operation.
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
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null. The exception is reported through the returned task.