Solved

lock table -commit rollback?

Posted on 2000-03-13
10
159 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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