Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is System.Transactions TransactionScope default Isolationlevel Serializable

I am just wondering what a good reason to use Serializable as the default Isolationlevel may be when creating a System.Transactions TransactionScope, because I cannot think of any (and it seems that you cannot change the default via web/app.config so you always have to set it in your code)

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

Instead I always have to write boilerplate code like this:

var txOptions = new System.Transactions.TransactionOptions(); txOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;  using(var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions))  {     ... } 

Any ideas?

like image 914
Bernhard Kircher Avatar asked Jul 02 '12 11:07

Bernhard Kircher


People also ask

Which is the default transaction isolation level?

The isolation level of the transactional support is default to READ UNCOMMITTED. You can change it to READ COMMITTED SNAPSHOT ISOLATION by turning ON the READ_COMMITTED_SNAPSHOT database option for a user database when connected to the master database.

What is set transaction isolation level Serializable?

Serializable Isolation Level. The Serializable isolation level provides the strictest transaction isolation. This level emulates serial transaction execution for all committed transactions; as if transactions had been executed one after another, serially, rather than concurrently.

What is the default isolation level in Entity Framework?

Accepted Answer. By default, a Transaction has an IsolationLevel of Serializable. Serializable is the highest level. It requires that the transaction completes before any other transaction is allowed to operate on the data.

Where is transaction isolation level set?

To set the transaction isolation level, use an ISOLATION LEVEL level clause. It is not permitted to specify multiple ISOLATION LEVEL clauses in the same SET TRANSACTION statement. The default isolation level is REPEATABLE READ . Other permitted values are READ COMMITTED , READ UNCOMMITTED , and SERIALIZABLE .


1 Answers

The fact Serializable is the default comes from times when .NET wasn't even released (before year 1999), from DTC (Distributed Transaction Coordinator) programming.

DTC uses a native ISOLATIONLEVEL enumeration:

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 is built on top of these technologies.

Now, the next question is: why DTC defines ISOLATIONLEVEL_SERIALIZABLE as the default transaction level? I suppose it's because DTC was designed around year 1995 (before 1999 for sure). At that time, the SQL Standard was SQL-92 (or SQL2).

And here is what SQL-92 says about transaction levels:

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.

like image 127
Simon Mourier Avatar answered Oct 13 '22 06:10

Simon Mourier