Deadlocks from calcsums
njhansen
Member Posts: 37
NAV 2009R2 on SQL
We're experiencing excessive deadlocking on our system, mostly because of additional load on the servers. When I went hunting for the most common queries that SQL was detecting as deadlocks, one that continually appeared was clearly from a calcsums (no matching flowfield):
SELECT SUM("SUM$<fieldname>") FROM dbo."<tablename>$VSIFT$39" WITH (UPDLOCK, ROWLOCK, NOEXPAND ) WHERE...
This intrigued me, since I don't typically see an UPDLOCK hint on calcfields/calcsums queries, so I hunted down the code that matches it, which goes something like this:
Rec1.LockTable;
...
Rec2.SetCurrentKey
Rec2.SetFilter
Rec2.CalcSums
Where Rec1 and Rec2 are separate record variables of type <tablename> in the above query.
Why is Rec1 lock causing a lock hint on Rec2?
Is there any way to prevent NAV from doing this?
Unfortunately, this code occurs in vendor code that is in a CU for which we only have execute privileges (so I can't easily change it).
We're experiencing excessive deadlocking on our system, mostly because of additional load on the servers. When I went hunting for the most common queries that SQL was detecting as deadlocks, one that continually appeared was clearly from a calcsums (no matching flowfield):
SELECT SUM("SUM$<fieldname>") FROM dbo."<tablename>$VSIFT$39" WITH (UPDLOCK, ROWLOCK, NOEXPAND ) WHERE...
This intrigued me, since I don't typically see an UPDLOCK hint on calcfields/calcsums queries, so I hunted down the code that matches it, which goes something like this:
Rec1.LockTable;
...
Rec2.SetCurrentKey
Rec2.SetFilter
Rec2.CalcSums
Where Rec1 and Rec2 are separate record variables of type <tablename> in the above query.
Why is Rec1 lock causing a lock hint on Rec2?
Is there any way to prevent NAV from doing this?
Unfortunately, this code occurs in vendor code that is in a CU for which we only have execute privileges (so I can't easily change it).
0
Answers
-
It is because you place the lock on a table, not on a certain instance of a record variable. After all, you want the lock to be effective even for other users.njhansen wrote:Why is Rec1 lock causing a lock hint on Rec2?
Is there any way to prevent NAV from doing this?
Unfortunately, this code occurs in vendor code that is in a CU for which we only have execute privileges (so I can't easily change it).
So this is a a lock on a single table. A lock on a single table can cause blocking, but you need locks on two or more tables to cause a deadlock. So to prevent it, check which tables are involved. Look after the sequence in which locks take effect. Can you identify the other code involved in the deadlock? Does it use the same locking sequence (this is essential)? Which code uses the recommended locking sequence?
Now either fix the one witch you have access to or ask the vendor to fix his.0 -
This is still heritage from the old Navision native database where we had table locking. If you do Locktable on one table varable it counts for variables from that table during the transaction.
I don't know if that is changed in newer versions, but in 2013 you can use a query object to work around it.0 -
Locking sift view records is absolutely correct way to quarantie that the sift amount doesn't change until the end of transaction.
Number of records affected depends on key and filters (flowfilter in case of calcfields).
For example, code below lock all sift records with specified location:ItemLedgerEntry.Locktable ItemLedgerEntry.setcurrentkey("Location Code"); ItemLedgerEntry.setrange("Location Code", rec."Location Code"); ItemLedgerEntry.calcsums(Quantity);Looking for part-time work.
Nav, T-SQL.0 -
Thanks for the replies.It is because you place the lock on a table, not on a certain instance of a record variable.
I figured that would be the answer, but wanted to check.If you do Locktable on one table varable it counts for variables from that table during the transaction.
The other contender in the deadlock is usually another instance of the same code. It is in the middle of a multiple stage update to the same table that it is scanning. If you look at the NAV code, everything seems entirely reasonable, so the vendor wasn't just being sloppy. It isn't until you look at the effect on SQL Server that things start to look bad.Can you identify the other code involved in the deadlock?
Interesting thought. We've used ADO connections to do some stuff in 2009 for which one would use a query object in 2013.in 2013 you can use a query object to work around it0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 324 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions

