Solved

lock table -commit rollback?

Posted on 2000-03-13
10
157 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

12 Experts available now in Live!

Get 1:1 Help Now