I have Navision 4.0 SP1 on SQL Server and I've this locking problem. Here an example that reproduce the problem:
2 users try to execute the code below, by applying different filters on the same table. Then a loop where filtered records are deleted is executed.
If the first user executes the DELETE loop, the second user is blocked.
CASE USERID OF
'user1' : MyTable.SETRANGE(Descr, 'uno');
'user2' : MyTable.SETRANGE(Descr, 'due');
END;
IF MyTable.FIND('-') THEN
REPEAT
MyTable1.GET(MyTable.Codice);
MyTable1.DELETE;
UNTIL MyTable.NEXT = 0;
IF CONFIRM('.........') THEN ;
Why this? SQL Server locks at the record level, is it true? Why this problem occours only with DELETE (the same with DELETEALL) and not with the MODIFY instruction?
Any ideas on how to solve this?
0
Comments
There is no way to know when SQL will decide to start with a tablelock.
The thing you might do is to do a COMMIT every N records. Negative of this is that the deleting will slow down because of the overhead of multiple transactions. And also if there is an error between the transactions, some of the records will have been deleted and others not.
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
So, are these problems not solvable?
If I check the Client Monitor, the SQL code generated for the NEXT instruction for the example on my post is this:
SELECT * FROM MyTable WITH (UPDLOCK, ROWLOCK) WHERE (("Descr"='uno')) AND "Codice">'1' ORDER BY "Codice" OPTION (FAST 80)
The clause "Codice">'1' seems to be the cause of the table lock. Any chance to avoid this?
This changes the isolation level in the SQL query, and should cause the whole table to be locked before it is blocked later.
RIS Plus, LLC
Can I force the DELETE instruction into che FIND cicle to lock only the single record and not the entire table?
It is not possible anyway to force SQL only to lock the records needed. Like it is possible SQL decides to upgrade recordlocks to tablelocks and we have no control about it.
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.