December 29, 2004 at 3:40 am
We're planning to develop an n-tier application at the moment using .NET framework and sql server technologies.
How should we manage locking in such a disconnected environment? If one user has selected a record for editing, all other users should be prevented from editing this record at this time. We don't want to tell the user "your record has been changed by anyone else" when he has completed his work. So there must be another solution. As an n-tier application has no durable connection to the database, the database locking techniques will not be useful.
I think everyone who develops n-tier applications has this problem. What are possible solutions?
December 29, 2004 at 8:27 am
Use a semaphore or flag on each record to note that it's locked, or better yet, the FK of the user ID that actually has it locked. So retrieve a record and mark it as "locked". When someone else goes to retrieve it, they'll know who is locking that record.
December 29, 2004 at 8:58 am
The method that Steve just described is the so-called Flag method. It works when the table involved is not large or the amount of users (concurrent) is not big. When the scale goes up then you will have to pretty much use the Locks table mechanism.
With the Locks table you just keep one table with TableName and PK of that row and create a Unique constraint on both columns. With that in mind every time a user wants to access a record it will try to inserted in this table first and if it is successful then you procceed otherwise inform client that is in use. at the end the client app will remove the record from this table.
The reasoning behind this strategy is that the locks table remains small comparetively to the rest of the tables so speed and concurrency issues are minimized.
HTH
* Noel
December 29, 2004 at 9:12 am
Two questions regarding these techniques:
1) Performance: are systems of these kind fast engough? Do you have such systems running in production environment?
2) Human factor: is there any way to automate the lock table strategy? IM shure some programmer will forget to set some locks if it isn't an automated feature. Problem ist that you have multiple records in mutiple tables to lock at some actions.
December 29, 2004 at 9:32 am
Markus,
1. By FAR when the transactional tables are large (+10 millions) and the number of users is large the Lockings are reduced DRAMATICALLY and it wont contend with people trying to read on those. Besides the exclusive locks will only occur on a small table (fast).Yes I have implemeted that as a Consultant a couple of times successfuly.
2. U are at a point in which the enviroment will determine the feasibility of this. It is very easy if your middle tier DAL is GENERATED (through templates, tools, scripts or whatever other mechanism) but if all is hand coded you are pretty much depending on coding standars an enforment of those (you will still have the same problem with the Flag method though 
The other apparent thing that I didn't mentioned is that this will avoid modification of all tables, you have a general mechanism which is portable(RDBMS independent), flexible (Could be applied to some tables not all) and has been proven on intense OLTP scenarios.
* Noel
December 29, 2004 at 9:40 am
OK. I think the lock table method is a good one. I considered it before this post but I wanted to heare some opinions on it before I make my descission.
Which tools do you use for generating the middle tier? Of cause I will have the same problem with the other approach. The only way to avoid this ist using other strategies such as optimistic locking ("The record you edited has been changed by another user ...").
So: which are the best practices for developing such applications?
December 29, 2004 at 10:00 am
The First questions are:
Are you expecting High Volume?
Are you expecting a lot of users concurrently?
Do you have appropriate hardware/budget?
etc...
About the tools
Some People use ApexSQL, some TierDeveloper, some Codesmith, other simply use sql scripts based on the schema with template files, etc. I didn't mentioned in any particular order you will have to do your homework and decide which may be more appropriate for your scenario/developers/standards
For best Practices look in the MS website for Patterns and Practices you will be able to find a wealth of recomendations there.
Remember, If the system is not expected to grow it may be ok to use the Flag method. At low levels of concurrency use the easier to implement.
HTH
* Noel
December 31, 2004 at 10:14 am
We've used the "Locks" table method to great success over the past few years, albeit in relatively small (~10 GB) databases with user populations in the dozens. Our application code for it sits in a middle tier auto-generated by tools developed in-house.
I've seen some large systems (older versions of Great Plains Dynamics, for instance) use the Flag method, however.
I like the idea of the Locks table for maintenance and administration, and it leaves your data model cleaner & smaller by not requiring a lock flag on each table.
If performance is a concern, you can always try pinning the lock table in memory.
February 9, 2005 at 11:21 pm
ok,
all this disscussion was really great, but what is the final outcome, if some 1 asks me how will u solve this problem
in an interview what should i answer him ??
<a href="http://www.websolsoftware.com"> For IT jobs click here</a>
*Sukhoi*[font="Arial Narrow"][/font]
March 8, 2005 at 1:51 pm
Hi i am trying to do do he same thing but please some give me example that if i query on table then how can i lock the table or record from other user?
Thanks
March 8, 2005 at 2:37 pm
To Use This method the access to the table has to be allowed only through stored procedures, then on the sp you will check for the Lock.
Cheers,
* Noel
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply