Method ExecuteTransactionConditional
- Namespace
- Open.Database.Extensions.Synchronous
- Assembly
- Open.Database.Extensions.MSSqlClient.dll
ExecuteTransactionConditional<T>(SqlConnection, Func<SqlTransaction, (bool Commit, T Value)>, IsolationLevel, CancellationToken)
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 (bool Commit, T Value) ExecuteTransactionConditional<T>(this SqlConnection connection, Func<SqlTransaction, (bool Commit, T Value)> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<SqlTransaction, (bool Commit, T Value)>The 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
- (bool Commit, T Value)
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
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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null.
ExecuteTransactionConditional(SqlConnection, Func<SqlTransaction, bool>, IsolationLevel, CancellationToken)
Runs the action in a transaction on the connection, committing it unless the action throws, returns false, or cancellationToken is canceled.
public static bool ExecuteTransactionConditional(this SqlConnection connection, Func<SqlTransaction, bool> conditionalAction, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<SqlTransaction, bool>The 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
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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null.
ExecuteTransactionConditional<T>(SqlConnection, Func<SqlTransaction, (bool Commit, T Value)>, CancellationToken)
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 (bool Commit, T Value) ExecuteTransactionConditional<T>(this SqlConnection connection, Func<SqlTransaction, (bool Commit, T Value)> conditionalAction, CancellationToken cancellationToken)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<SqlTransaction, (bool Commit, T Value)>The 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
- (bool Commit, T Value)
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
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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null.
ExecuteTransactionConditional(SqlConnection, Func<SqlTransaction, bool>, CancellationToken)
Runs the action in a transaction on the connection, committing it unless the action throws, returns false, or cancellationToken is canceled.
public static bool ExecuteTransactionConditional(this SqlConnection connection, Func<SqlTransaction, bool> conditionalAction, CancellationToken cancellationToken)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
conditionalActionFunc<SqlTransaction, bool>The 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
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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null.
ExecuteTransactionConditional<T>(SqlConnection, IsolationLevel, CancellationToken, Func<SqlTransaction, (bool Commit, T Value)>)
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 (bool Commit, T Value) ExecuteTransactionConditional<T>(this SqlConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<SqlTransaction, (bool Commit, T Value)> conditionalAction)
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.
conditionalActionFunc<SqlTransaction, (bool Commit, T Value)>The handler to execute while the transaction is pending. Returning a
Commitvalue of true signals to commit the transaction.
Returns
- (bool Commit, T Value)
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
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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null.
ExecuteTransactionConditional(SqlConnection, IsolationLevel, CancellationToken, Func<SqlTransaction, bool>)
Runs the action in a transaction on the connection, committing it unless the action throws, returns false, or cancellationToken is canceled.
public static bool ExecuteTransactionConditional(this SqlConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<SqlTransaction, bool> conditionalAction)
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.
conditionalActionFunc<SqlTransaction, bool>The handler to execute while the transaction is pending. Returning true signals to commit the transaction.
Returns
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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null.
ExecuteTransactionConditional<T>(SqlConnection, IsolationLevel, Func<SqlTransaction, (bool Commit, T Value)>, CancellationToken)
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 (bool Commit, T Value) ExecuteTransactionConditional<T>(this SqlConnection connection, IsolationLevel isolationLevel, Func<SqlTransaction, (bool Commit, T Value)> conditionalAction, CancellationToken cancellationToken = default)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
conditionalActionFunc<SqlTransaction, (bool Commit, T Value)>The 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
- (bool Commit, T Value)
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
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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null.
ExecuteTransactionConditional(SqlConnection, IsolationLevel, Func<SqlTransaction, bool>, CancellationToken)
Runs the action in a transaction on the connection, committing it unless the action throws, returns false, or cancellationToken is canceled.
public static bool ExecuteTransactionConditional(this SqlConnection connection, IsolationLevel isolationLevel, Func<SqlTransaction, bool> conditionalAction, CancellationToken cancellationToken = default)
Parameters
connectionSqlConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
conditionalActionFunc<SqlTransaction, bool>The 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
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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionorconditionalActionis null.