Method ExecuteTransactionConditionalAsync
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
ExecuteTransactionConditionalAsync<T>(DbConnection, Func<DbTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>>, IsolationLevel, CancellationToken)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this DbConnection connection, Func<DbTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<DbTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>>The asynchronous handler to execute while the transaction is pending. It receives the transaction and
cancellationToken. Returning aCommitvalue of true signals to commit the transaction.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<(bool Commit, T Value)>
A task that represents the asynchronous operation; its result is the value returned by
conditionalAction. ItsCommitvalue is true if the transaction was committed.
Type Parameters
TThe type of the value returned by the action.
Remarks
The transaction is committed if the action does not throw, its Commit value is true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync<T>(DbConnection, Func<DbTransaction, ValueTask<(bool Commit, T Value)>>, IsolationLevel, CancellationToken)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this DbConnection connection, Func<DbTransaction, ValueTask<(bool Commit, T Value)>> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<DbTransaction, ValueTask<(bool Commit, T Value)>>The asynchronous handler to execute while the transaction is pending. Returning a
Commitvalue of true signals to commit the transaction.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<(bool Commit, T Value)>
A task that represents the asynchronous operation; its result is the value returned by
conditionalAction. ItsCommitvalue is true if the transaction was committed.
Type Parameters
TThe type of the value returned by the action.
Remarks
The transaction is committed if the action does not throw, its Commit value is true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
conditionalActionis null.- ArgumentNullException
connectionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync(DbConnection, Func<DbTransaction, ValueTask<bool>>, IsolationLevel, CancellationToken)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<bool> ExecuteTransactionConditionalAsync(this DbConnection connection, Func<DbTransaction, ValueTask<bool>> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<DbTransaction, ValueTask<bool>>The asynchronous handler to execute while the transaction is pending. Returning true signals to commit the transaction.
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<bool>
A task that represents the asynchronous operation; its result is true if the transaction was committed; otherwise, false.
Examples
The transaction is committed only if exactly one row was deleted:
bool committed = await connection.ExecuteTransactionConditionalAsync(
async transaction =>
{
int rows = await transaction
.Command("DELETE FROM Posts WHERE Id = @id AND AuthorId = @authorId")
.AddParam("id", postId)
.AddParam("authorId", authorId)
.ExecuteNonQueryAsync(cancellationToken);
return rows == 1;
},
cancellationToken);
Remarks
The transaction is committed if the action does not throw, returns true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync<T>(DbConnection, Func<DbTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>>, CancellationToken)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this DbConnection connection, Func<DbTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>> conditionalAction, CancellationToken cancellationToken)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<DbTransaction, CancellationToken, ValueTask<(bool Commit, T Value)>>The asynchronous handler to execute while the transaction is pending. It receives the transaction and
cancellationToken. Returning aCommitvalue of true signals to commit 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<(bool Commit, T Value)>
A task that represents the asynchronous operation; its result is the value returned by
conditionalAction. ItsCommitvalue is true if the transaction was committed.
Type Parameters
TThe type of the value returned by the action.
Remarks
The transaction is committed if the action does not throw, its Commit value is true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync<T>(DbConnection, Func<DbTransaction, ValueTask<(bool Commit, T Value)>>, CancellationToken)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this DbConnection connection, Func<DbTransaction, ValueTask<(bool Commit, T Value)>> conditionalAction, CancellationToken cancellationToken)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<DbTransaction, ValueTask<(bool Commit, T Value)>>The asynchronous handler to execute while the transaction is pending. Returning a
Commitvalue of true signals to commit 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<(bool Commit, T Value)>
A task that represents the asynchronous operation; its result is the value returned by
conditionalAction. ItsCommitvalue is true if the transaction was committed.
Type Parameters
TThe type of the value returned by the action.
Remarks
The transaction is committed if the action does not throw, its Commit value is true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
conditionalActionis null.- ArgumentNullException
connectionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync(DbConnection, Func<DbTransaction, ValueTask<bool>>, CancellationToken)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<bool> ExecuteTransactionConditionalAsync(this DbConnection connection, Func<DbTransaction, ValueTask<bool>> conditionalAction, CancellationToken cancellationToken)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<DbTransaction, ValueTask<bool>>The asynchronous handler to execute while the transaction is pending. Returning true signals to commit 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<bool>
A task that represents the asynchronous operation; its result is true if the transaction was committed; otherwise, false.
Examples
The transaction is committed only if exactly one row was deleted:
bool committed = await connection.ExecuteTransactionConditionalAsync(
async transaction =>
{
int rows = await transaction
.Command("DELETE FROM Posts WHERE Id = @id AND AuthorId = @authorId")
.AddParam("id", postId)
.AddParam("authorId", authorId)
.ExecuteNonQueryAsync(cancellationToken);
return rows == 1;
},
cancellationToken);
Remarks
The transaction is committed if the action does not throw, returns true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync<T>(DbConnection, IsolationLevel, CancellationToken, Func<DbTransaction, ValueTask<(bool Commit, T Value)>>)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this DbConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<DbTransaction, ValueTask<(bool Commit, T Value)>> conditionalAction)
Parameters
connectionDbConnectionThe 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.
conditionalActionFunc<DbTransaction, ValueTask<(bool Commit, T Value)>>The asynchronous handler to execute while the transaction is pending. Returning a
Commitvalue 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. ItsCommitvalue is true if the transaction was committed.
Type Parameters
TThe type of the value returned by the action.
Remarks
The transaction is committed if the action does not throw, its Commit value is true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
conditionalActionis null.- ArgumentNullException
connectionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync(DbConnection, IsolationLevel, CancellationToken, Func<DbTransaction, ValueTask<bool>>)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<bool> ExecuteTransactionConditionalAsync(this DbConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<DbTransaction, ValueTask<bool>> conditionalAction)
Parameters
connectionDbConnectionThe 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.
conditionalActionFunc<DbTransaction, 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.
Examples
The transaction is committed only if exactly one row was deleted:
bool committed = await connection.ExecuteTransactionConditionalAsync(
async transaction =>
{
int rows = await transaction
.Command("DELETE FROM Posts WHERE Id = @id AND AuthorId = @authorId")
.AddParam("id", postId)
.AddParam("authorId", authorId)
.ExecuteNonQueryAsync(cancellationToken);
return rows == 1;
},
cancellationToken);
Remarks
The transaction is committed if the action does not throw, returns true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync<T>(DbConnection, IsolationLevel, Func<DbTransaction, ValueTask<(bool Commit, T Value)>>, CancellationToken)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<(bool Commit, T Value)> ExecuteTransactionConditionalAsync<T>(this DbConnection connection, IsolationLevel isolationLevel, Func<DbTransaction, ValueTask<(bool Commit, T Value)>> conditionalAction, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
conditionalActionFunc<DbTransaction, ValueTask<(bool Commit, T Value)>>The asynchronous handler to execute while the transaction is pending. Returning a
Commitvalue of true signals to commit 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<(bool Commit, T Value)>
A task that represents the asynchronous operation; its result is the value returned by
conditionalAction. ItsCommitvalue is true if the transaction was committed.
Type Parameters
TThe type of the value returned by the action.
Remarks
The transaction is committed if the action does not throw, its Commit value is true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
conditionalActionis null.- ArgumentNullException
connectionis null. The exception is reported through the returned task.
ExecuteTransactionConditionalAsync(DbConnection, IsolationLevel, Func<DbTransaction, ValueTask<bool>>, CancellationToken)
Asynchronously begins a transaction on the connection, invokes the action, and commits or rolls back the transaction.
public static ValueTask<bool> ExecuteTransactionConditionalAsync(this DbConnection connection, IsolationLevel isolationLevel, Func<DbTransaction, ValueTask<bool>> conditionalAction, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
conditionalActionFunc<DbTransaction, ValueTask<bool>>The asynchronous handler to execute while the transaction is pending. Returning true signals to commit 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<bool>
A task that represents the asynchronous operation; its result is true if the transaction was committed; otherwise, false.
Examples
The transaction is committed only if exactly one row was deleted:
bool committed = await connection.ExecuteTransactionConditionalAsync(
async transaction =>
{
int rows = await transaction
.Command("DELETE FROM Posts WHERE Id = @id AND AuthorId = @authorId")
.AddParam("id", postId)
.AddParam("authorId", authorId)
.ExecuteNonQueryAsync(cancellationToken);
return rows == 1;
},
cancellationToken);
Remarks
The transaction is committed if the action does not throw, returns true,
and cancellationToken has not been canceled; otherwise it is rolled back.
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. 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 (for example Microsoft.Data.Sqlite and MySqlConnector) only roll back an uncommitted transaction on dispose while the connection is still open. 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, and the connection
closed with CloseAsync, where the target has them (not
netstandard2.0, which does both synchronously).
The token is checked just before the commit, and the commit itself is not
canceled once it has begun: cancelling part-way through would leave the
outcome unknown.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null. The exception is reported through the returned task.