gpt4 book ai didi

c# - 为什么 System.Transactions TransactionScope 默认 Isolationlevel Serializable

转载 作者:IT王子 更新时间:2023-10-29 03:42:44 29 4
gpt4 key购买 nike

我只是想知道在创建 System.Transactions 时,使用 Serializable 作为默认隔离级别的一个很好的理由是什么? TransactionScope,因为我想不出任何(而且你似乎无法通过 web/app.config 更改默认值,所以你总是必须在你的代码中设置它)

using(var transaction = TransactionScope()) 
{
... //creates a Transaction with Serializable Level
}

相反,我总是必须像这样编写样板代码:

var txOptions = new System.Transactions.TransactionOptions();
txOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

using(var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions))
{
...
}

有什么想法吗?

最佳答案

事实Serializable默认值来自 .NET 甚至未发布的时代(1999 年之前),来自 DTC (Distributed Transaction Coordinator) 编程。

DTC 使用 native ISOLATIONLEVEL枚举:

ISOLATIONLEVEL_SERIALIZABLE Data read by a current transaction cannot be changed by another transaction until the current transaction finishes. No new data can be inserted that would affect the current transaction. This is the safest isolation level and is the default, but allows the lowest level of concurrency.

.NET TransactionScope建立在这些技术之上。

现在,下一个问题是:为什么 DTC 定义 ISOLATIONLEVEL_SERIALIZABLE作为默认交易级别?我想这是因为 DTC 是在 1995 年左右设计的(肯定是在 1999 年之前)。当时,SQL 标准是 SQL-92(或 SQL2)。

这就是SQL-92说到交易水平:

An SQL-transaction has an isolation level that is READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, or SERIALIZABLE. The isolation level of an SQL-transaction defines the degree to which the operations on SQL-data or schemas in that SQL-transaction are affected by the effects of and can affect operations on SQL-data or schemas in concurrent SQL-transactions. The isolation level of a SQL- transaction is SERIALIZABLE by default. The level can be explicitly set by the <set transaction statement>.

The execution of concurrent SQL-transactions at isolation level SERIALIZABLE is guaranteed to be serializable. A serializable execution is defined to be an execution of the operations of concurrently executing SQL-transactions that produces the same effect as some serial execution of those same SQL-transactions. A serial execution is one in which each SQL-transaction executes to completion before the next SQL-transaction begins.

关于c# - 为什么 System.Transactions TransactionScope 默认 Isolationlevel Serializable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11292763/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com