![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||||
| |||||
|
|
Sql-Server 2000, 2005. A report fetches a lot of rows using the "WITH (ROWLOCK)" syntax (the sql is generated on the fly by a tool and not easily changeable). SELECT col1, col2 FROM mytab WITH (ROWLOCK) WHERE ...." The select-clause runs for several minutes. Another user fetches one of those rows and tries to update it. The result is a lock timeout. |
|
I suppose that the long running select-clause has put a shared lock on the rows and the updater (exclusive-lock) will have to wait for the long-running select and so the lock timeout is expiring. |
|
Are all those rows "shared locked" until all are fetched? Would there be any change if the "WITH (ROWLOCK)" is removed, isn't although "shared lock" the default behaviour? |
|
The "WITH (NOLOCK)" would probably help? |
|
What about the definition of optimistic concurrency, shouldn't all select-clauses contain "WITH (NOLOCK)" to allow an optimistic concurrency scenario? |
#3
| |||||
| |||||
|
|
What settings in Sql-Server are you thinking of? |
|
Do you mean that by default the updater will wait until the long running sql-clause is ready as the lock timeout is by default infinite? |
|
But do we agree that the default behaviour is that, if you don't use (nolock), the select-clause puts a shared lock on the rows fetched? |
|
Most reports need the situation at a specific time, and there isn't a problem if some rows are being updated at the moment, the value before or after the updating will do. |
|
Here a definition: ***Optimistic concurrency means you read the database record, but don't lock it. Anyone can read and modify the record at anytime and you will take your chances that the record is not modified by someone else before you have a chance to modify and save it. As a developer, the burden is on you to check for changes in the original data ( collisions ) and act accordingly based on any errors that may occur during the update.*** But how can an application use optimistic cocurrency (which contains: read the database record, but don't lock it) without (NOLOCK), |
#4
| |||
| |||
|
|
Here a definition: ***Optimistic concurrency means you read the database record, but don't lock it. *Anyone can read and modify the record at anytime and you will take your chances that the record is not modified by someone else before you have a chance to modify and save it. *As a developer, the burden is on you to check for changes in the original data ( collisions ) and act accordingly based on any errors that may occur during the update.*** But how can an application use optimistic cocurrency (which contains: read the database record, but don't lock it) without (NOLOCK), This is a different context. Imagine a function that reads information from the database and displays to the user, and permits the user to update the data. Pessimistic concurrency calls for the row being locked while the user has it on the screen. Optimistic concurrency means that you do hold locks while waiting for user input. You still lock the rows when you actually read the data. but that usually only takes an instant. /Erland |
#5
| |||
| |||
|
|
So running long lasting reports will stronly negatively affect the whole concept of optimistic concurrency. Actually the updater can't update the row as the report has put a "shared lock" on the rows. |
|
Is using some kind of Olap-server (old data gathered) a better way than (nolock) in a realtime environment? |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
Thanks Erland for very good answers. I think that snapshot isolation is the answer to this problem. Unfortunately I'm using a tool generating the SQL so there will be a lot of problems to accomplish the change, especially as the tool is supporting several different databases. But we have to live with this and try to minimize the "length" of the select-clauses and perhaps investigate how long the tool is holding a shared lock on the rows. I suppose that there would't be any improvement to use a view, or is a view a way to simulate snapshot isolation? |
#8
| |||
| |||
|
|
I don't think you have to meddle with the tool at all. It can be as simple as * * ALTER DATABASE db READ_COMMITTED_SNAPSHOT ON From this point and on the isolation level READ COMMITTED will be implemented with the snapshot technique. Thus, this does not affect your queries at all. But your locking issues will be gone. |
#9
| |||
| |||
|
|
Sounds great! So then we will just have to ask our customers to hurry up updating to Sql Server 2005 (or later). |
#10
| |||
| |||
|
|
(Roger.Noreply (AT) gmail (DOT) com) writes: Sounds great! So then we will just have to ask our customers to hurry up updating to Sql Server 2005 (or later). If they are in hurry, they should stay at SQL 2005, as SQL 2005 will not ship until Q3. :-) |
![]() |
| Thread Tools | |
| Display Modes | |
| |