Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change isolation level?

I am using EF 4.0, and I would like to use the isolation level serializable, because in a transaction I would like to block a register when is read.

Well, in SQL Server I try to change the isolation level with this command:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

And in C# I use this code to try to block the register:

using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.Serializable }))
{
   Entities myContext = new Entities();
   string colors = "select * from Colors where IDColor = 2";
   Colors myColor = myContext.Colors.SqlQuery(colors).FirstOrDefault<Colors>();
   myColor.Color = "Red";
   myContext.SaveChanges();
   scope.Complete();                  
}

If I execute line by line my code, if I get the SaveChanges but still is not execute, if I do a query to the table colors with SQL Server Management Studio for example, I can get the record.

Then, if I execute the save changes and I get the scope.Clompete(), the if I try the query with Management Studio, I get nothing because the register is blocked, so when I finish my C# code, the register is release and I get the result in Management Studio.

So my question is, how to use the serializable isolation level in C#? am I doing something wrong?

P.S.: I notice that if I use this command:

DBCC useroptions;

I see that the isolation level is serializable, but if I close Management Studio and connect again and see the isolation level is the default readCommitted.

So my question is how to set the default isolation level for my database.

like image 431
Álvaro García Avatar asked Jan 26 '13 12:01

Álvaro García


People also ask

How do I change the isolation level in SQL?

How do I do this? From the tools menu select options. Under Query Execution/SQL Server/Advanced, change the value of SET TRANSACTION ISOLATION LEVEL to READ UNCOMMITTED.

What is the best isolation level?

The highest isolation level, serializable, guarantees that a transaction will retrieve exactly the same data every time it repeats a read operation. But it uses a level of locking that is likely to impact other users in multi-user systems.

What is isolation level in SQL?

Isolation is the I in the acronym ACID; the isolation level is the setting that fine-tunes the balance between performance and reliability, consistency, and reproducibility of results when multiple transactions are making changes and performing queries at the same time.


1 Answers

  • Default EF transaction isolation level is based on used database provider.

  • Unspecified isolation level in your ef code should result in default isolation level for database server.

  • In SQL Server default isolation level is READ COMMITED.

  • So you don't need to specify IsolationLevel on your EF code.If you set it on DB side it takes as default IsolationLevel for EF as well.

How to change IsolationLevel on DB Check Isolation Levels in the Database Engine and SET TRANSACTION ISOLATION LEVEL (Transact-SQL)

UPDATE

For change the isolation level run below mentioned command on SSMS :

USE YourDatabaseName;
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

For check whether is it applied ?

USE YourDatabaseName;
GO
DBCC useroptions

MSDN says:

Only one of the isolation level options can be set at a time, and it remains set for that connection until it is explicitly changed. All read operations performed within the transaction operate under the rules for the specified isolation level unless a table hint in the FROM clause of a statement specifies different locking or versioning behavior for a table.

I hope this will help to you.

like image 181
Sampath Avatar answered Oct 11 '22 05:10

Sampath