Solved

SQL Server + Locks

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

21 Experts available now in Live!

Get 1:1 Help Now