Solved

lock table -commit rollback?

Posted on 2000-03-13
10
158 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to implement a User Shortcut mapper? 1 95
Printing problem 2 81
delphi parse string to params 3 100
LAN or WAN ? 11 83
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now