Table of Contents

Method ExecuteTransaction

Namespace
Open.Database.Extensions.Synchronous
Assembly
Open.Database.Extensions.MSSqlClient.dll

ExecuteTransaction<T>(SqlConnection, Func<SqlTransaction, T>, IsolationLevel, CancellationToken)

Runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.

public static T ExecuteTransaction<T>(this SqlConnection connection, Func<SqlTransaction, T> action, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

action Func<SqlTransaction, T>

The handler to execute while the transaction is pending.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A 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

T

The 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

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or action is null.

ExecuteTransaction(SqlConnection, Action<SqlTransaction>, IsolationLevel, CancellationToken)

Runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.

public static void ExecuteTransaction(this SqlConnection connection, Action<SqlTransaction> action, IsolationLevel isolationLevel = IsolationLevel.Unspecified, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

action Action<SqlTransaction>

The handler to execute while the transaction is pending.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A 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. 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

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or action is null.

ExecuteTransaction<T>(SqlConnection, Func<SqlTransaction, T>, CancellationToken)

Runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.

public static T ExecuteTransaction<T>(this SqlConnection connection, Func<SqlTransaction, T> action, CancellationToken cancellationToken)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

action Func<SqlTransaction, T>

The handler to execute while the transaction is pending.

cancellationToken CancellationToken

A 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

T

The 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

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or action is null.

ExecuteTransaction(SqlConnection, Action<SqlTransaction>, CancellationToken)

Runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.

public static void ExecuteTransaction(this SqlConnection connection, Action<SqlTransaction> action, CancellationToken cancellationToken)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

action Action<SqlTransaction>

The handler to execute while the transaction is pending.

cancellationToken CancellationToken

A 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. 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

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or action is null.

ExecuteTransaction<T>(SqlConnection, IsolationLevel, CancellationToken, Func<SqlTransaction, T>)

Runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.

public static T ExecuteTransaction<T>(this SqlConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Func<SqlTransaction, T> action)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A 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.

action Func<SqlTransaction, T>

The handler to execute while the transaction is pending.

Returns

T

The value returned by the action.

Type Parameters

T

The 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

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or action is null.

ExecuteTransaction(SqlConnection, IsolationLevel, CancellationToken, Action<SqlTransaction>)

Runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.

public static void ExecuteTransaction(this SqlConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken, Action<SqlTransaction> action)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

isolationLevel IsolationLevel

The isolation level for the transaction.

cancellationToken CancellationToken

A 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.

action Action<SqlTransaction>

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. 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

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or action is null.

ExecuteTransaction<T>(SqlConnection, IsolationLevel, Func<SqlTransaction, T>, CancellationToken)

Runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.

public static T ExecuteTransaction<T>(this SqlConnection connection, IsolationLevel isolationLevel, Func<SqlTransaction, T> action, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

isolationLevel IsolationLevel

The isolation level for the transaction.

action Func<SqlTransaction, T>

The handler to execute while the transaction is pending.

cancellationToken CancellationToken

A 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

T

The 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

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or action is null.

ExecuteTransaction(SqlConnection, IsolationLevel, Action<SqlTransaction>, CancellationToken)

Runs the action in a transaction on the connection, committing it unless the action throws or cancellationToken is canceled.

public static void ExecuteTransaction(this SqlConnection connection, IsolationLevel isolationLevel, Action<SqlTransaction> action, CancellationToken cancellationToken = default)

Parameters

connection SqlConnection

The connection to begin the transaction on. It does not need to be open.

isolationLevel IsolationLevel

The isolation level for the transaction.

action Action<SqlTransaction>

The handler to execute while the transaction is pending.

cancellationToken CancellationToken

A 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. 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

cancellationToken was canceled before the transaction was committed.

ArgumentNullException

connection or action is null.