Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does SqlConnection manage IsolationLevel?

This MSDN article states that:

An isolation level has connection-wide scope, and once set for a connection with the SET TRANSACTION ISOLATION LEVEL statement, it remains in effect until the connection is closed or another isolation level is set. When a connection is closed and returned to the pool, the isolation level from the last SET TRANSACTION ISOLATION LEVEL statement is retained. Subsequent connections reusing a pooled connection use the isolation level that was in effect at the time the connection is pooled.

The SqlConnection class has no member that may hold the isolation level. So how does a connection know what isolation level to run in???

The reason I'm asking this is because of the following scenario:

  1. I opened a transaction using TransactionScope in Serializable mode, say "T1".
  2. Opened a connection for T1.
  3. T1 is finished/disposed, connection goes back to connection pool.
  4. Called another query on same connection (after getting it from connection pool) and this query runs in serializable mode!!!

Problem:

  1. How does the pooled connection still know what isolation level was associated to it???
  2. How to revert it back to some other transaction level???

Resolution:
The reason why pooled connections are returning the serializable isolation level is because of the following reason:

  1. You have one connection pool (let's say CP1)
  2. CP1 may have 50 connections.
  3. You pick one connection C1 from CP1 and execute it with Serializable. This connection has its isolation level set now. Whatever you do, this will not be reset (unless this connection is used to execute a code in a different isolation level).
  4. After executing the query C1(Serializable) goes back to CP1.
  5. If steps 1-4 are executed again then the connection used may be some other connection than C1, let's say C2 or C3. So, that will also have its isolation level set to Serializable.
  6. So, slowly, Serialzable is set to multiple connections in CP1.
  7. When you execute a query where no explicit isolation level setting is being done, the connection picked from CP1 will decide the isolation level. For e.g. if such a query requests for a connection and CP1 uses C1(Serializable) to execute this query then this query will execute in Serializable mode even though you didn't explicitly set it.

Hope that clears a few doubts. :)

like image 795
Sidharth Panwar Avatar asked Sep 21 '10 11:09

Sidharth Panwar


People also ask

How does transaction isolation work?

Transactions specify an isolation level that defines how one transaction is isolated from other transactions. Isolation is the separation of resource or data modifications made by different transactions. Isolation levels are described for which concurrency side effects are allowed, such as dirty reads or phantom reads.

How is isolation achieved in database?

Thus, in order to achieve perfect isolation, all the system has to do is to ensure that when transactions are running concurrently, the final state is equivalent to a state of the system that would exist if they were run serially.

How does SQL isolation level work?

SQL Server provides 5 Isolation levels to implement with SQL Transaction to maintain data concurrency in the database. Isolation level is nothing but locking the row while performing some task, so that other transaction can not access or will wait for the current transaction to finish its job.

What is isolation level in transaction management?

The transaction isolation level is a state within databases that specifies the amount of data that is visible to a statement in a transaction, specifically when the same data source is accessed by multiple transactions simultaneously.


1 Answers

Isolation levels are implemented in the underlying DBMS, say SqlServer. Setting the isolation level most probably sets up SQL commands which set the isolation level for the connection.

The DBMS keeps the isolation level as long as the connection stays open. Because the connections is put into the pool, it stays open and keeps the settings made before.

When messing around with isolation levels, you should either reset the isolation level at the end of any transaction, or, even better, set it when a new connection is requested.

like image 136
Stefan Steinegger Avatar answered Sep 18 '22 16:09

Stefan Steinegger