Method ExecuteTransactionConditional
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
ExecuteTransactionConditional<T>(DbConnection, Func<DbTransaction, (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 (bool Commit, T Value) ExecuteTransactionConditional<T>(this DbConnection connection, Func<DbTransaction, (bool Commit, T Value)> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to transact with.
conditionalActionFunc<DbTransaction, (bool Commit, T Value)>The handler to execute while a transaction is pending. Returning a 'Commit' value of true signals to commit the transaction.
isolationLevelIsolationLevelThe isolation level for the transaction.
cancellationTokenCancellationTokenA token that if cancelled will cause this transaction to be aborted or rolled-back.
Returns
Type Parameters
TThe 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.
ExecuteTransactionConditional(DbConnection, Func<DbTransaction, bool>, IsolationLevel, CancellationToken)
Begins a transaction before executing the action. Commits if there are no exceptions, the conditional action returns true, and the optional cancellation token is not cancelled. Otherwise rolls-back the transaction.
public static bool ExecuteTransactionConditional(this DbConnection connection, Func<DbTransaction, bool> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to transact with.
conditionalActionFunc<DbTransaction, bool>The handler to execute while a transaction is pending. Returning true signals to commit the transaction.
isolationLevelIsolationLevelThe isolation level for the transaction.
cancellationTokenCancellationTokenA token that if cancelled will cause this transaction to be aborted or rolled-back.
Returns
- bool
True if committed.
ExecuteTransactionConditional<T>(DbConnection, Func<DbTransaction, (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 (bool Commit, T Value) ExecuteTransactionConditional<T>(this DbConnection connection, Func<DbTransaction, (bool Commit, T Value)> conditionalAction, CancellationToken cancellationToken)
Parameters
connectionDbConnectionThe connection to transact with.
conditionalActionFunc<DbTransaction, (bool Commit, T Value)>The handler to execute while a transaction is pending. Returning a 'Commit' value of true signals to commit the transaction.
cancellationTokenCancellationTokenA token that if cancelled will cause this transaction to be aborted or rolled-back.
Returns
Type Parameters
TThe 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.
ExecuteTransactionConditional(DbConnection, Func<DbTransaction, bool>, CancellationToken)
Begins a transaction before executing the action. Commits if there are no exceptions, the conditional action returns true, and the optional cancellation token is not cancelled. Otherwise rolls-back the transaction.
public static bool ExecuteTransactionConditional(this DbConnection connection, Func<DbTransaction, bool> conditionalAction, CancellationToken cancellationToken)
Parameters
connectionDbConnectionThe connection to transact with.
conditionalActionFunc<DbTransaction, bool>The handler to execute while a transaction is pending. Returning true signals to commit the transaction.
cancellationTokenCancellationTokenA token that if cancelled will cause this transaction to be aborted or rolled-back.
Returns
- bool
True if committed.
ExecuteTransactionConditional<T>(DbConnection, IsolationLevel, CancellationToken, Func<DbTransaction, (bool Commit, T Value)>)
Begins a transaction before executing the action. Commits if there are no exceptions and 'Commit' value from the action is true. Otherwise rolls-back the transaction.
public static (bool Commit, T Value) ExecuteTransactionConditional<T>(this DbConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<DbTransaction, (bool Commit, T Value)> conditionalAction)
Parameters
connectionDbConnectionThe connection to transact with.
isolationLevelIsolationLevelThe isolation level for the transaction.
cancellationTokenCancellationTokenA token that if cancelled will cause this transaction to be aborted or rolled-back.
conditionalActionFunc<DbTransaction, (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
Type Parameters
TThe value returned from the action.
ExecuteTransactionConditional(DbConnection, IsolationLevel, CancellationToken, Func<DbTransaction, bool>)
Begins a transaction before executing the action. Commits if there are no exceptions and the conditional action returns true. Otherwise rolls-back the transaction.
public static bool ExecuteTransactionConditional(this DbConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<DbTransaction, bool> conditionalAction)
Parameters
connectionDbConnectionThe connection to transact with.
isolationLevelIsolationLevelThe isolation level for the transaction.
cancellationTokenCancellationTokenA token that if cancelled will cause this transaction to be aborted or rolled-back.
conditionalActionFunc<DbTransaction, bool>The handler to execute while a transaction is pending. Returning true signals to commit the transaction.
Returns
- bool
True if committed.
ExecuteTransactionConditional<T>(DbConnection, IsolationLevel, Func<DbTransaction, (bool Commit, T Value)>, CancellationToken)
Begins a transaction before executing the action. Commits if there are no exceptions and 'Commit' value from the action is true. Otherwise rolls-back the transaction.
public static (bool Commit, T Value) ExecuteTransactionConditional<T>(this DbConnection connection, IsolationLevel isolationLevel, Func<DbTransaction, (bool Commit, T Value)> conditionalAction, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to transact with.
isolationLevelIsolationLevelThe isolation level for the transaction.
conditionalActionFunc<DbTransaction, (bool Commit, T Value)>The handler to execute while a transaction is pending. Returning a 'Commit' value of true signals to commit the transaction.
cancellationTokenCancellationTokenAn optional token that if cancelled will cause this transaction to be aborted or rolled-back.
Returns
Type Parameters
TThe value returned from the action.
ExecuteTransactionConditional(DbConnection, IsolationLevel, Func<DbTransaction, bool>, CancellationToken)
Begins a transaction before executing the action. Commits if there are no exceptions and the conditional action returns true. Otherwise rolls-back the transaction.
public static bool ExecuteTransactionConditional(this DbConnection connection, IsolationLevel isolationLevel, Func<DbTransaction, bool> conditionalAction, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to transact with.
isolationLevelIsolationLevelThe isolation level for the transaction.
conditionalActionFunc<DbTransaction, bool>The handler to execute while a transaction is pending. Returning true signals to commit the transaction.
cancellationTokenCancellationTokenAn optional token that if cancelled will cause this transaction to be aborted or rolled-back.
Returns
- bool
True if committed.
ExecuteTransactionConditional<T>(DbConnection, CancellationToken, Func<DbTransaction, (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 cancellation token has not been cancelled. Otherwise rolls-back the transaction.
public static (bool Commit, T Value) ExecuteTransactionConditional<T>(this DbConnection connection, CancellationToken cancellationToken, Func<DbTransaction, (bool Commit, T Value)> conditionalAction)
Parameters
connectionDbConnectionThe connection to transact with.
cancellationTokenCancellationTokenA token that if cancelled will cause this transaction to be aborted or rolled-back.
conditionalActionFunc<DbTransaction, (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
Type Parameters
TThe value returned from the action.
ExecuteTransactionConditional(DbConnection, CancellationToken, Func<DbTransaction, bool>)
Begins a transaction before executing the action. Commits if there are no exceptions, the conditional action returns true, and the optional cancellation token is not cancelled. Otherwise rolls-back the transaction.
public static bool ExecuteTransactionConditional(this DbConnection connection, CancellationToken cancellationToken, Func<DbTransaction, bool> conditionalAction)
Parameters
connectionDbConnectionThe connection to transact with.
cancellationTokenCancellationTokenAn optional token that if cancelled will cause this transaction to be aborted or rolled-back.
conditionalActionFunc<DbTransaction, bool>The handler to execute while a transaction is pending. Returning true signals to commit the transaction.
Returns
- bool
True if committed.