Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

lock table -commit rollback?

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
RBertora
Asked:
RBertora
  • 5
  • 3
  • 2
1 Solution
 
kretzschmarCommented:
? 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
 
RBertoraAuthor Commented:
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
 
ITugayCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RBertoraAuthor Commented:
Hi Igor,
I like your solution very much,
did you think of that solution yourself?
Rob.


0
 
ITugayCommented:
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
 
RBertoraAuthor Commented:
Adjusted points to 200
0
 
RBertoraAuthor Commented:
In that case I am doubling the points on ths question:
well done!

Rob.
0
 
ITugayCommented:
Thanx a lot, Rob.

Best regards,
Igor.
0
 
RBertoraAuthor Commented:
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
 
kretzschmarCommented:
yup, is ok ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now