473,890 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

More than pessimistic record locking needed...

Actually, it is not only the record locking, what I need, and nobody seems
to descibe this.

Imagine the following scenario.
There is a database with, say 10000 records with some unvalidated data. And
there is an Intranet ASP.NET application which is an interface to the
database in question... and there are 100 pretty girls eager to... uhmm...
use the application and validate the data of course ;-).

The task is to enable the data validation in such a way that two girls NEVER
get the same record for validation (it is just a waste of time). When a girl
presses 'next' or something like that, she gets the access to an unvalidated
(and not being validated) record. This is more than pessimistic locking,
actually. And I missed the most important part - all this has to work
smoothly ;-).

I am thinking about a few solutions to this problem, I developed one once,
actually, basing on DB transactions with highest isolation level, but this
was not very smooth. Anyway, if anybody can give me a good advice or direct
to a good article, I will be very grateful.

Keep in mind that I might use synchronisation NOT on the DB level, but on
the level of the web application... maybe this would be faster?

Specifically, I think that I might keep a specialised locking object, say
"theLocker" in the application cache, to keep the hashtable with locked
records and users that locked them and incorporate some logic on this
hashtable.

I would use something like (I am writing in C#):
lock (theLocker) {
// lock / unlock the desired record
}

This is just an idea... what do you think about it?

Any comments will be welcome
DW.

Jun 27 '06 #1
9 2663
How about a simple suggestion!

Have you thought about each client marking a record as taken, so no select
yet - just an update to say that record is mine for however long I need it.
The client can generate a GUID or timestamp to place inside the record and
any record with an exiting GUID is off limits. When your next client wants
a record for validation, they update the next record that is not allocated a
GUID with their own GUID. That way they can select that record as many
times as they like with no interference from any other client and it matters
little that your client may be in a world disconnected from your database.
Of course you'll need to think about releasing records from dead clients but
thats a different problem.

--
Regards

John Timney (MVP)
"master" <ma****@master. com> wrote in message
news:uq******** ******@TK2MSFTN GP02.phx.gbl...
Actually, it is not only the record locking, what I need, and nobody seems
to descibe this.

Imagine the following scenario.
There is a database with, say 10000 records with some unvalidated data.
And
there is an Intranet ASP.NET application which is an interface to the
database in question... and there are 100 pretty girls eager to... uhmm...
use the application and validate the data of course ;-).

The task is to enable the data validation in such a way that two girls
NEVER
get the same record for validation (it is just a waste of time). When a
girl
presses 'next' or something like that, she gets the access to an
unvalidated
(and not being validated) record. This is more than pessimistic locking,
actually. And I missed the most important part - all this has to work
smoothly ;-).

I am thinking about a few solutions to this problem, I developed one once,
actually, basing on DB transactions with highest isolation level, but this
was not very smooth. Anyway, if anybody can give me a good advice or
direct
to a good article, I will be very grateful.

Keep in mind that I might use synchronisation NOT on the DB level, but on
the level of the web application... maybe this would be faster?

Specifically, I think that I might keep a specialised locking object, say
"theLocker" in the application cache, to keep the hashtable with locked
records and users that locked them and incorporate some logic on this
hashtable.

I would use something like (I am writing in C#):
lock (theLocker) {
// lock / unlock the desired record
}

This is just an idea... what do you think about it?

Any comments will be welcome
DW.

Jun 27 '06 #2
Provided I understood you correctly, I tried a similar solution in fact.

All my tables have an 'ID' field, so I added an additional tLocks table
whose records contained a user name, a table name and a record ID. Then
handling the 'next' button was something like:

- begin a transaction,
- delete a previous tLocks record for a current user, if it existed,
- select first not yet validated record from the required table for which no
record exists in tLocks,
- add a new tLocks record for the current user, the requested table and the
appropriate record ID,
- commit the transaction.

Looks OK, doesn't it?

Well, while using the default transaction level, it was very commonplace
that two users happened to lock the same record. Changing the level to the
highest possible value seemed to help... for 2-4 users. When 10 users used
the database simultaneously, they again happened to get the same records at
a time :-/.

Well, I do not understand why this happened, but it did ;-).

Frankly speaking, I would be glad to read some article providing a few
possible solutions to choose from. Or maybe a description of some system
that was tested and worked under a real stress...

DW.

Jun 27 '06 #3
Adding a timeout checking to my solution seems quite easy - a datetime field
should be added to tLocks table, each record will be added with the current
time. Each transaction would then start with deleting records that are
timed-out, i.e. the records whose datetime field is older than a session
timeout...

That would be nice, provided the whle solution worked ;-). But the users
reported it did not :-/.

This is why I am thinking about handling the locking not by the DB but by
the web application instead, using a global application cache and a lock
statement. I assume that each http session has a separate thread, am I
right?

Additionally, this solution should be a lot faster than blocking the
database with highly isolated transactions...

DW

Jun 27 '06 #4
you don't say which database you are using but you are running into
concurrency issues. if two (or more) users run your sql at the same time,
they both get the same unvalidated record, and both see its not in the locks
table, then both try to add it. then transactions logic only guarantees that
if the insert fails, the delete will be rolled back.

you should move the select and insert into one statement (though depending
on the database vendor this may not work). use a table lock or set the
transaction isolation level serialiable. see the lock options for your
vendor. what you need to do is take an exclusive lock at the start of your
transaction to prevent others fromn reading the table during your
transaction).

-- bruce (sqlwork.com)
"master" <ma****@master. com> wrote in message
news:u8******** ******@TK2MSFTN GP04.phx.gbl...
Provided I understood you correctly, I tried a similar solution in fact.

All my tables have an 'ID' field, so I added an additional tLocks table
whose records contained a user name, a table name and a record ID. Then
handling the 'next' button was something like:

- begin a transaction,
- delete a previous tLocks record for a current user, if it existed,
- select first not yet validated record from the required table for which
no
record exists in tLocks,
- add a new tLocks record for the current user, the requested table and
the
appropriate record ID,
- commit the transaction.

Looks OK, doesn't it?

Well, while using the default transaction level, it was very commonplace
that two users happened to lock the same record. Changing the level to the
highest possible value seemed to help... for 2-4 users. When 10 users used
the database simultaneously, they again happened to get the same records
at
a time :-/.

Well, I do not understand why this happened, but it did ;-).

Frankly speaking, I would be glad to read some article providing a few
possible solutions to choose from. Or maybe a description of some system
that was tested and worked under a real stress...

DW.

Jun 27 '06 #5

"master" <ma****@master. com> wrote in message
news:u$******** ******@TK2MSFTN GP03.phx.gbl...
Adding a timeout checking to my solution seems quite easy - a datetime
field
should be added to tLocks table, each record will be added with the
current
time. Each transaction would then start with deleting records that are
timed-out, i.e. the records whose datetime field is older than a session
timeout...
Potential progress then!
That would be nice, provided the whle solution worked ;-). But the users
reported it did not :-/. This is why I am thinking about handling the locking not by the DB but by
the web application instead, using a global application cache and a lock
statement. I assume that each http session has a separate thread, am I
right?
Each session has its own thread safe object but I'm not sure holding
thousands of records in application or session is a good idea. Thats a lot
of work for a collection when a database is probably better optimised for
this type of processing.
Additionally, this solution should be a lot faster than blocking the
database with highly isolated transactions...


Yes - perhaps. More than 1 query can return a select so you can almost
guarentee at some point on a heavy database that you will get concurrency
issues, and you would essentially be recreating db functionality in asp.net.
With the correct locking inside a transaction SQL Server places an exclusive
lock on the page (or the row) so unique marking should be possible - leaving
you free to select exclusively and not worry about update problems due to
work in progress time passing, and reducing your memory overhead in
asp.net..

If I was you, I'd pop into one of the SQL server newsgroups and ask their
advice.

--
Regards

John Timney (MVP)

Jun 27 '06 #6
> you don't say which database you are using but you are running into
concurrency issues.
MS SQL Server.

if two (or more) users run your sql at the same time,
they both get the same unvalidated record, and both see its not in the locks table, then both try to add it. then transactions logic only guarantees that if the insert fails, the delete will be rolled back.
Yes, that was exactly the behaviour I observed while using the default
isolation level.
However, even when I changed it to 'serializable', some users reported that
they have problems getting the same records, when the sytem was under a
stress. I do not understand why... maybe I just do not understand the
concept of a transaction enough...

what you need to do is take an exclusive lock at the start of your
transaction to prevent others fromn reading the table during your
transaction).


Frankly speaking, I do not know how to lock the table in MS SQL Server,
though I searched the books online for this...
I am much more an application programmer than a DB programmer...

DW

Jun 28 '06 #7
> > Each transaction would then start with deleting records that are
timed-out, i.e. the records whose datetime field is older than a session
timeout...
Potential progress then!


What do you mean? Could you comment more on this?

Each session has its own thread safe object but I'm not sure holding
thousands of records in application or session is a good idea. Thats a lot of work for a collection when a database is probably better optimised for
this type of processing.
Actually, there will never be THAT many. I do not think more than 100 users
will use the application at the same time.

If I was you, I'd pop into one of the SQL server newsgroups and ask their
advice.


I did ;-)

DW

Jun 28 '06 #8
"master" <ma****@master. com> wrote in message
news:uU******** ******@TK2MSFTN GP02.phx.gbl...
> Each transaction would then start with deleting records that are
> timed-out, i.e. the records whose datetime field is older than a
> session
> timeout...


Potential progress then!


What do you mean? Could you comment more on this?


I only mean that this at least appears to be something you have cracked and
therefore dont have to worry about!
Each session has its own thread safe object but I'm not sure holding
thousands of records in application or session is a good idea. Thats a

lot
of work for a collection when a database is probably better optimised for
this type of processing.


Actually, there will never be THAT many. I do not think more than 100
users
will use the application at the same time.


If that is the case, then for so few users, I would certainly look at
updating the field as I suggested, and keeping your solution very simple
indeed. If you cant get it working with that simple approach then the
application object and its lock method would help you maintain some
concurrency independence I expect. Dont even bother with session objects.
If I was you, I'd pop into one of the SQL server newsgroups and ask their
advice.


I did ;-)


Hope they offerered some good advice, theres some very techncial people in
there.
Regards

John Timney (MVP)
Jun 28 '06 #9
> > > > Each transaction would then start with deleting records that are
> timed-out, i.e. the records whose datetime field is older than a
> session timeout...
Potential progress then! What do you mean? Could you comment more on this?

I only mean that this at least appears to be something you have cracked

and therefore dont have to worry about!
Now I understand even less than before ;-)
I thought you found some danger in the strategy I had presented... which I
am not aware of. Then I would like to know it.

Hope they offerered some good advice, theres some very techncial people in
there.


Well, they directed me what to search for as far as the locking of data is
concerned... this does not seem to be well documented in the SQL Server
books online. There are no examples, especially...

DW.

Jun 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4049
by: Fardude | last post by:
ACCESS 97, Pessimistic Record Locking!??? Does Access 97 allow record level Pessimistic locking? In other words, when user A is editing a record (has it locked) and User B tries to edit it (attempts to lock it), user B will get an error saying this record cannot be locked now. It seems like Access 97 is doing optimistic record level locking. In above scenario user B, is allowed to edit the record but when tries to
6
5693
by: DebbieG | last post by:
I have created a database for a client and was told that it was to be a one-user database. Well, you know the next statement ... now they want 3 people to be able to use the database. (FYI, I have never created a database for multiusers. I've done some searching but not finding what I want.) I have split the database. In Tools, Options, I have set the following: Default open mode = Shared Default record locking = Edited Record
2
2519
by: Patrick Fisher | last post by:
Hi To display a message when a user attempts to edit a record in a multi-user environment where a forms Record Locking is set to Edited Record and another user is editing a record, is difficult because Access does not trigger an error, I have overcome this problem by trapping the Delete and Backspace key in the KeyDown event of the form and displaying a label on the form saying "If Record cannot be Edited it may be that Another User is...
13
1685
by: Jeff Davis | last post by:
Right now performance isn't a problem, but this question has me curious: Let's say I have a shopping cart system where there is a "products" table that contains all possible products, and an "cart_items" table that stores how many of each product are in each cart. The obvious (or the first thing that came to my mind) would look something like this: create table products (
22
18825
by: RayPower | last post by:
I'm having problem with using DAO recordset to append record into a table and subsequent code to update other tables in a transaction. The MDB is Access 2000 with the latest service pack of JET 4. The system is client/server, multiusers based. The MDBs are using record locking. Here is part of the code: Dim wkSpace As Workspace, db As Database Dim rstTrans As DAO.Recordset Set wkSpace = DBEngine.Workspaces(0)
4
5674
by: misterutterbag | last post by:
Hi. Given the following: ---------------- String lockSubscriptionQuery = "SELECT subscriptionId FROM subscription WHERE subscriptionId = ? FOR UPDATE"; pStmt = conn.prepareStatement(lockSubscriptionQuery);
1
2806
by: DaveP | last post by:
How do i do Pessimistic locking in ado.net with c# Thanks DaveP
1
2232
by: Paul H | last post by:
I have an Employees table with the following fields: EmployeeID SupervisorID Fred Bob Bob John Bob Mary Bill Bill I have created a self join in the relationships window, with
1
3217
Dököll
by: Dököll | last post by:
Hey Ladies and Gents! I would like to permit only one user edit to a row in a database. I am a firm believer at having the db do most of the work but the app I am maintaining could use some robustness; as a couple of users were picked up to have updated the same record, at the same time. Do you think locking the database per user instance (which I have no idea about, as of yet) is a better approach than locking/disabling the update button...
0
9816
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11218
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10910
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8009
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4667
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4260
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.