Solved

lock table -commit rollback?

Posted on 2000-03-13
10
160 Views
Last Modified: 2010-04-04
I asked this question in MSSQL section of exchange, but sofar not much help from there... so perhaps someone here has the answer:

Hi, I am writing a program that needs to farm out work to various
operators. Each operator is going to work on one record at a time,
they will click next, and the next record that is of status 1 will
be displayed to them, while they are modifying this record the status
needs to be set to 2, such that another operator does not also work
on the same record: This is a simplified version of what is going on
but it illustrates my problem:

There is a table called work

work
======
ID
Workfields..
Status

Firstly I run "Select * from work where status = 1 order by ID" =RecordSet
This brings back a list of work to be done.
Now I need to run an update:
"update work set status = 2 where ID = IDofFirstInRecordSet"

The problem is I need both queries to run asif they were one query,
ie atomic...(otherwise another operator might just run the select statement
before the original operator has updated the record and thus both of them
work on the same record!!)

 I don't know how to achieve this.. can any one help

Rob.
0
Comment
Question by:RBertora
  • 5
  • 3
  • 2
10 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2611858
? hmm,

hi rob,
well thats not so easy, because i don't know much from mssql, but i guess there is a function for explicit locking.

well, i've another way in mind.
what about with an additional onwork(ed)table. a table which holds the id's of the records and maybe the status (both bound as PK or UK), where you just insert your recordID and status. if another want do the same, a exception comes up (constraint error), because another was first with inserting.

meikl
0
 
LVL 7

Author Comment

by:RBertora
ID: 2611868
Hi Meikl,
I have thought of that.. but there is a problem : I need to run two statements even for that:

1. select * from work where status = 1

2. insert first record into newtable..

if I combine the whole thing into an insert.. it won't work because I just can't get the first record, I get all the records where status =1... do you follow me?

Rob.
0
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
ID: 2611970
Hi RBertora,

you need to prevent all SELECTS from table "works" to all operators if one of them run SELECT from "works". Right? But it look like impossible by this way.      You need not know not only "is record locked", but you need to know "who did it". Can you set status = id_user?

/* browse work table */
1. select * from work where status=1.

/* try to set lock on desired record */
2. update work set status=:id_user where id=IDofFirstInRecordSet and status=1
/* if somebody lock record while you thinking about, nothing happen, right?*/

/* try open record for editing */
3. select * from work where  status=:id_user and  id=IDofFirstInRecordSet
/* if another operator lock record, you will retirieve empty resultset, and you can say "you are late!" and return to browsing */

/* unlock */
4. update work set status=1 where id=IDofFirstInRecordSet


Best regards,
Igor.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Author Comment

by:RBertora
ID: 2612018
Hi Igor,
I like your solution very much,
did you think of that solution yourself?
Rob.


0
 
LVL 9

Expert Comment

by:ITugay
ID: 2612040
Hi Rob,

Yes, I did it by myself;)

Cheers,
Igor.

PS:if you have TCP/IP then you can use last digits from IP address as ID_USER.
0
 
LVL 7

Author Comment

by:RBertora
ID: 2612046
Adjusted points to 200
0
 
LVL 7

Author Comment

by:RBertora
ID: 2612047
In that case I am doubling the points on ths question:
well done!

Rob.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2612058
Thanx a lot, Rob.

Best regards,
Igor.
0
 
LVL 7

Author Comment

by:RBertora
ID: 2612066
Hey its not often you get original thought on a question these days, and your solution is spot on.
my pleasure
keep well
Rob.

P.S. Meikl I think you might still be watching this question... like the answer?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2612112
yup, is ok ;-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

856 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