SQL Server transactions and transaction isolation - getting errors that I don't know how to fix

Taudris

I have an ASP.NET MVC application using EF6 and SQL Server with up to 15 or so concurrent users. To ensure the consistency of data between different queries during each page request, I have everything enclosed in transactions (using System.Transactions.TransactionScope).

When I use IsolationLevel.ReadCommitted and .Serializable, I get deadlock errors like this:

Transaction (Process ID #) was deadlocked on lock resources with another process and has been chosen as the deadlock victim.

When I use IsolationLevel.Snapshot, I get errors like this:

Snapshot isolation transaction aborted due to update conflict. You cannot use snapshot isolation to access table 'dbo.#' directly or indirectly in database '#' to update, delete, or insert the row that has been modified or deleted by another transaction. Retry the transaction or change the isolation level for the update/delete statement.

These errors are the least frequent when using IsolationLevel.Snapshot (one to three per day, roughly).

My understanding of the issue leads me to believe that the only ways to guarantee zero transaction failures is to either:

  1. Completely serialize all database access, or
  2. Implement some type of transaction retry functionality.

And I can't do 1 because some tasks and requests take a while to run, while other parts of the application need to stay reasonably responsive.

I'm inclined to think retry could be implemented by getting MVC to re-run the controller action, but I don't know how to go about doing such a thing.

I also don't know how to reproduce the errors that my users are causing. All I get right now are rather uninformative exception logs. I could set up EF to log all SQL that gets run on the DB, now that EF6 lets you do that, but I'm not sure how helpful that would actually be.

Any ideas?

CRAFTY DBA

Regardless of isolation level, there are two categories of locks. EXCLUSIVE for INSERT, DELETE, UPDATE and shared for SELECT.

You should try the limit the transaction time for EXCLUSIVE locks to a minimum. The default isolation level is READ COMMITTED. If you are writing/running reports against the OLTP systems, writers will block readers. You might get blocking issues.

In 2005, READ COMMITTED SNAPSHOT ISOLATION was introduced. For readers, the version store in tempdb is used to capture a snapshot of the data to satisfy the current query. A-lot less overhead than SNAPSHOT ISOLATION. In short readers are now not blocked by writers.

This should fix your blocking issues. You need to remove any table hints or isolation commands you currently have.

See article from Brent Ozar.

http://www.brentozar.com/archive/2013/01/implementing-snapshot-or-read-committed-snapshot-isolation-in-sql-server-a-guide/

Will it fix your deadlock? Probably not.

Deadlocks are causes by two or more resources exclusive lock in opposite order.

Check out MSDN = way cooler pictures and mentions deadlock flags.

http://technet.microsoft.com/en-us/library/ms178104(v=sql.105).aspx

Process 1
DEBIT BANK ACCOUNT
CREDIT VENDOR ACCOUNT

Process 2
CREDIT VENDOR ACCOUNT
DEBIT BANK ACCOUNT

In short, change the order of your DML to have consistent access to the tables. Turn on a trace flag to get the actual TSQL causing the issue.

Last but not least, check out application locks as a last resort. The can be used as MUTEX's on code that might be causing deadlocks.

http://www.sqlteam.com/article/application-locks-or-mutexes-in-sql-server-2005

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

double multiplication is getting rounded, and i don't know how to fix it

From Dev

I don't know how to fix this

From Dev

I have the following errors appearing on my code, I don't know what they mean neither know how to fix them

From Dev

I keep getting subscript out of range don't know how to fix it

From Dev

PDO Error I don't know how to fix

From Dev

My element is out of screen and I don't know how to fix it

From Dev

PDO Error I don't know how to fix

From Dev

SQL Server transaction isolation and atomicity

From Dev

Trying to compile X Window System and getting errors that I don't know

From Dev

php error - don't know how to fix it

From Dev

php error - don't know how to fix it

From Dev

Fatal error in Xcode - I know the cause but don't know how to fix it

From Dev

I've been trying to implement and AVL Tree but I keep on getting these 2 errors C2954 and C2955 and don't know how to solve them

From Dev

Getting an "ArgumentOutOfRangeException" error but don't know how?

From Dev

Getting an "ArgumentOutOfRangeException" error but don't know how?

From Dev

I got some errors when i start a tomcat server in idea, i don't know how to resolve it.It's the log information below

From Dev

I have two NullPointerException in Processing, both animation thread errors that I don't know how to resolve

From Dev

Broken xfce4 applications menu entry - I know where the executable is but I don't know how to fix it

From Dev

My Windows 10 taskbar icons are messed up and I don't know how to fix it

From Dev

Javascript/CSS slideshow: Using transparencies highlights flaws I don't know how to fix

From Dev

Creating a game using Jquery, huge issue involving an object 'removing' another that I don't know how to fix

From Dev

My partitions are messed up and I don't know how to fix them

From Dev

I keep getting "signal 1: SIGABRT" in swift 3 and I don't know how to get around it

From Dev

I keep getting a segmentation fault and I don't know why

From Dev

I keep getting this error message and I don't know why

From Java

I don't know how to use JPQL [AND] and [OR]

From Dev

I don't know how to make this function

From Dev

AdventureWorks activation error in SQL Server, I don't know if it is my pathway is wrong or my database is wrong

From Dev

Don't know how to fix my PropertyChangeListener on a JFormattedTextField

Related Related

  1. 1

    double multiplication is getting rounded, and i don't know how to fix it

  2. 2

    I don't know how to fix this

  3. 3

    I have the following errors appearing on my code, I don't know what they mean neither know how to fix them

  4. 4

    I keep getting subscript out of range don't know how to fix it

  5. 5

    PDO Error I don't know how to fix

  6. 6

    My element is out of screen and I don't know how to fix it

  7. 7

    PDO Error I don't know how to fix

  8. 8

    SQL Server transaction isolation and atomicity

  9. 9

    Trying to compile X Window System and getting errors that I don't know

  10. 10

    php error - don't know how to fix it

  11. 11

    php error - don't know how to fix it

  12. 12

    Fatal error in Xcode - I know the cause but don't know how to fix it

  13. 13

    I've been trying to implement and AVL Tree but I keep on getting these 2 errors C2954 and C2955 and don't know how to solve them

  14. 14

    Getting an "ArgumentOutOfRangeException" error but don't know how?

  15. 15

    Getting an "ArgumentOutOfRangeException" error but don't know how?

  16. 16

    I got some errors when i start a tomcat server in idea, i don't know how to resolve it.It's the log information below

  17. 17

    I have two NullPointerException in Processing, both animation thread errors that I don't know how to resolve

  18. 18

    Broken xfce4 applications menu entry - I know where the executable is but I don't know how to fix it

  19. 19

    My Windows 10 taskbar icons are messed up and I don't know how to fix it

  20. 20

    Javascript/CSS slideshow: Using transparencies highlights flaws I don't know how to fix

  21. 21

    Creating a game using Jquery, huge issue involving an object 'removing' another that I don't know how to fix

  22. 22

    My partitions are messed up and I don't know how to fix them

  23. 23

    I keep getting "signal 1: SIGABRT" in swift 3 and I don't know how to get around it

  24. 24

    I keep getting a segmentation fault and I don't know why

  25. 25

    I keep getting this error message and I don't know why

  26. 26

    I don't know how to use JPQL [AND] and [OR]

  27. 27

    I don't know how to make this function

  28. 28

    AdventureWorks activation error in SQL Server, I don't know if it is my pathway is wrong or my database is wrong

  29. 29

    Don't know how to fix my PropertyChangeListener on a JFormattedTextField

HotTag

Archive