Read
Uncommitted
This is the lowest isolation level there
is. Read uncommitted causes no shared locks to be requested which allows you to
read data that is currently being modified in other transactions. It also
allows other transactions to modify data that you are reading. As you can
probably imagine this can cause some unexpected results in a variety of
different ways. For example data returned by the select could be in a half way
state if an update was running in another transaction causing some of your rows
to come back with the updated values and some not to.
Read
Committed
This is the default isolation level and
means selects will only return committed data. Select statements will issue
shared lock requests against data you’re querying this causes you to wait if
another transaction already has an exclusive lock on that data. Once you have
your shared lock any other transactions trying to modify that data will request
an exclusive lock and be made to wait until your Read Committed transaction
finishes.
Repeatable
Read
This is similar to Read Committed but with
the additional guarantee that if you issue the same select twice in a
transaction you will get the same results both times. It does this by holding
on to the shared locks it obtains on the records it reads until the end of the
transaction. This means any transactions that try to modify these records are
forced to wait for the read transaction to complete.
Serializable
This isolation level takes Repeatable Read
and adds the guarantee that no new data will be added eradicating the chance of
getting Phantom Reads. It does this by placing range locks on the queried data.
This causes any other transactions trying to modify or insert data touched on
by this transaction to wait until it has finished.
Snapshot
This provides the same guarantees as
Serializable. So what’s the difference? Well it’s more in the way it works;
using snapshot doesn’t block other queries from inserting or updating the data
touched by the snapshot transaction. Instead row versioning is used so when
data is changed the old version is kept in tempdb so existing transactions will
see the version without the change. When all transactions that started before
the changes are complete the previous row version is removed from tempdb. This
means that even if another transaction has made changes you will always get the
same results as you did the first time in that transaction.
So on the plus side; you are not blocking
anyone else from modifying the data whilst you run your transaction but….
You’re using extra resources on the SQL Server to hold multiple versions of
your changes. To use the snapshot isolation level you need to enable it on the
database by running the following command
ALTER DATABASE IsolationTests
SET ALLOW_SNAPSHOT_ISOLATION ON
If you enable SNAPSHOT isolation on a database, SQL Server starts implementing versioning technologies so that queries can set their isolation level to snapshot. As soon as you enable SNAPSHOT isolation on a database, SQL Server waits for running transactions to complete, then immediately starts using versioning for data modifications. You start using an extra 14 bytes per row on tables in the database itself. Also, versions are created in the tempdb version store to hold the previous value of data for updates, deletes, and some inserts. This happens even if no queries are run using SNAPSHOT isolation.
·
If
a transaction having read committed isolation level or snapshot isolation
levels, it allows other transactions to update that table. Because, after the
select statement, it will release the lock on table.
·
If
a transaction having repeatable read isolation level, it does not allows other
transactions to update that table. because, after the select statement, it will
not release the lock on table till the transaction is completed
No comments:
Post a Comment