Solved

SQL Server + Locks

Posted on 2007-04-03
6
340 Views
Last Modified: 2008-06-28
I'm using SQL SERVER 2000  with .Net
I want
When  ONE User   EDIT  the  RECORD,  Other User  Should not be allowed  to EDIT or DELETE  that Record.

Kindly  help me to   to  do it    using   Locking  techniques.
Thanks.
0
Comment
Question by:sohrabus
  • 2
6 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 18848391
0
 
LVL 42

Expert Comment

by:dqmq
ID: 18848408
What you're referring to is called pessimistic locking. Before heading that direction, be sure to research the tradeoffs.  First, it's not easy to do with .NET because of its inherently disconnected architecture. In fact, if you're coding up a browser page, pessimistic locking is impossible with .NET and you would need to implement it external to your data provider.  Second, it suffers from performance issues and does not scale well.  Here's some links describe some of the issues and show you how to do it.

http://builder.com.com/5100-6388-1049842.html
http://www.dotnet247.com/247reference/msgs/24/124232.aspx
http://jbknet.blogspot.com/2006/03/dealing-with-concurrency-issues-in.html



0
 

Author Comment

by:sohrabus
ID: 18848529
Dear dgmq,
I'm sorry, I forgot to write one thing.

Right now I'm developing   Window Based Application(not web based)  using  .Net   and i want to implement it in my Window Based Application.
Pls give some advice.
0
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 18852360
For a windows based application it is possible.  If you simply use connection and command objects, then it's pretty straight forward because you have a lot of control. So, it would be just like doing it in TSQL, except the commands originate from the client.

1. Open a connection
2. Start a transaction on the connection
3. Set the LOCK_TIMEOUT to the longest time you want somebody to wait. Otherwise, they can get blocked indefinately.
4. Send a SELECT ... FOR UPDATE statement or invoke a stored procedure that does same.  The FOR UPDATE causes locks to be issued.

5.  Present the record to the user until "Submit" (or whatever) is pressed.  Note the connection MUST remain open (And may remain open for a very long time, which is one reason this approach doesn't scale).

6. Send the UPDATE statement or invoke a stored procedure the does same. It is essential that you use the same connection that you started in step 2.
7. COMMIT or ROLLBACK transaction (which releases locks).
8. Close Connection

You can also do it using more complex .NET objects, such as Table Adapters, that encapsulate the basic command/connection objects.  However, you don't have as much control and must be careful to "finesse" the backend so that the connection remains open across iterations. You also need to understand that Table Adapters are oriented toward working on lots of records at the same time, which may not be a good thing if they are all locked. The links given in my previous post go into more detail about how to do this.

Personally, for pessimistic locking, I'd stick with the first method, since I don't like trusting the internal nuances of how those advanced objects manage connections.



 



 
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question