Solved

SQL Server + Locks

Posted on 2007-04-03
6
336 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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now