Method ExecuteTransaction
- Namespace
- Open.Database.Extensions.Synchronous
- Assembly
- Open.Database.Extensions.Core.dll
ExecuteTransaction<T>(DbConnection, Func<DbTransaction, T>, IsolationLevel, CancellationToken)
Begins a transaction on the connection, invokes the action, and commits it
unless the action throws or cancellationToken is canceled.
public static T ExecuteTransaction<T>(this DbConnection connection, Func<DbTransaction, T> action, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
actionFunc<DbTransaction, T>The 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
- T
The value returned by the 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. 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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null.
ExecuteTransaction(DbConnection, Action<DbTransaction>, IsolationLevel, CancellationToken)
Begins a transaction on the connection, invokes the action, and commits it
unless the action throws or cancellationToken is canceled.
public static void ExecuteTransaction(this DbConnection connection, Action<DbTransaction> action, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
actionAction<DbTransaction>The 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.
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. 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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null.
ExecuteTransaction<T>(DbConnection, Func<DbTransaction, T>, CancellationToken)
Begins a transaction on the connection, invokes the action, and commits it
unless the action throws or cancellationToken is canceled.
public static T ExecuteTransaction<T>(this DbConnection connection, Func<DbTransaction, T> action, CancellationToken cancellationToken)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
actionFunc<DbTransaction, T>The 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
- T
The value returned by the 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. 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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null.
ExecuteTransaction(DbConnection, Action<DbTransaction>, CancellationToken)
Begins a transaction on the connection, invokes the action, and commits it
unless the action throws or cancellationToken is canceled.
public static void ExecuteTransaction(this DbConnection connection, Action<DbTransaction> action, CancellationToken cancellationToken)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
actionAction<DbTransaction>The 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.
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. 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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null.
ExecuteTransaction<T>(DbConnection, IsolationLevel, CancellationToken, Func<DbTransaction, T>)
Begins a transaction on the connection, invokes the action, and commits it
unless the action throws or cancellationToken is canceled.
public static T ExecuteTransaction<T>(this DbConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<DbTransaction, T> action)
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.
actionFunc<DbTransaction, T>The handler to execute while the transaction is pending.
Returns
- T
The value returned by the 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. 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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null.
ExecuteTransaction(DbConnection, IsolationLevel, CancellationToken, Action<DbTransaction>)
Begins a transaction on the connection, invokes the action, and commits it
unless the action throws or cancellationToken is canceled.
public static void ExecuteTransaction(this DbConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Action<DbTransaction> action)
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.
actionAction<DbTransaction>The handler to execute while the transaction is pending.
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. 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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null.
ExecuteTransaction<T>(DbConnection, IsolationLevel, Func<DbTransaction, T>, CancellationToken)
Begins a transaction on the connection, invokes the action, and commits it
unless the action throws or cancellationToken is canceled.
public static T ExecuteTransaction<T>(this DbConnection connection, IsolationLevel isolationLevel, Func<DbTransaction, T> action, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
actionFunc<DbTransaction, T>The 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
- T
The value returned by the 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. 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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null.
ExecuteTransaction(DbConnection, IsolationLevel, Action<DbTransaction>, CancellationToken)
Begins a transaction on the connection, invokes the action, and commits it
unless the action throws or cancellationToken is canceled.
public static void ExecuteTransaction(this DbConnection connection, IsolationLevel isolationLevel, Action<DbTransaction> action, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection to begin the transaction on. It does not need to be open.
isolationLevelIsolationLevelThe isolation level for the transaction.
actionAction<DbTransaction>The 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.
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. 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.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the transaction was committed.- ArgumentNullException
connectionoractionis null.