Solved

SQL Server + Locks

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS  - Parameters with comma 10 36
Remove () 9 35
Datatable / Dates ? 4 25
Sql server query 8 16
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

738 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