Solved

SQL Server + Locks

Posted on 2007-04-03
6
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

688 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