How can I safely exit a DBMS when records are locked?

I am a hobby programmer, but have been doing this for a while. I am writing a small document management application for use at work (in C #). When a user opens a record, I update it to indicate that it is currently locked for editing.

What I'm not sure about how to ensure the database is updated when the application crashes (for example, the computer shuts down unexpectedly)? In addition, how can I update it when the application leaves the computer that the user disconnects? I just want to make sure that I don’t have entries marked as locked when no one is viewing them.

+3
source share
5 answers

This is how it is usually done with SQL Server. The "write locks" developed by the developer are not related to the client-server architecture. You confuse the shared file architecture with the client-server architecture.

Make sure the table has a timestamp column (which is automatically updated by the database engine).

Read on the line you want to edit. Put a timestamp from a string into a variable.

The update operation looks like this:

update myTable
set col = {some value}
where id = {your id}
AND
timestampcolumn = {the timestamp the row had when you read it in}

If someone changed the line after you read it, it will have a different timestamp, and no record will meet the conditions of the WHERE clause, and therefore your update will fail. Then you can decide what to do.

, SQL-Server ( Oracle -), .

+2

# try-catch-finally . ( , ).

, , IDisposable, , . , ( ), -

using (RecordLockingThing myThing = new RecordLockingThing())
{
    //DoStuff
}
//Now myThing is out of scope, and will have been disposed.

, RecordLockingThing .

, , , ( ). . - , , , , , " " " ( ), .

, , , , , .

+1

, ; () LockOwnerId LockExpiry, . , , , , . - .

; , , , p

0

DMS, , , 5 . , , , , , . , .

0

, , , sp_getapplock, . ( , ), . (, ), , SQL Server, .

, SQL Server , .

As I said, one slight touch to it is that you must keep the connection to the server open. Therefore, it may be more suitable, say, for 50 customers, and not if you have 1000 customers. I will also add one more caution - I did not create a production system using these tools.

0
source

Source: https://habr.com/ru/post/1774937/


All Articles