Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Ways to sequence calls from multiple processes

by DrWhy (Chaplain)
on Nov 28, 2004 at 14:19 UTC ( [id://410800]=note: print w/replies, xml ) Need Help??


in reply to Re: Ways to sequence calls from multiple processes
in thread Ways to sequence calls from multiple processes

You could - for example - have a one-row table with a status field that each process tries to update like UPDATE mytable SET status=1 WHERE status=0 LIMIT 1. After each update you check the number of affected rows; it it's 1 you got access to the resource (and atomically locked it), if no row was affected you could not obtain the access, so you sleep() random seconds and try again. To release the resource you simply UPDATE mytable SET status = 0 WHERE table_id = 123
bart's criticisms of this approach are well taken, however, there are ways to take advantage of database locking to solve your problem. In Oracle, for example, you could have the same table described, but do a SELECT FOR UPDATE mytable or even just LOCK mytable. Then you would do your work on your resource. When you are finished, do a ROLLBACK or an UNLOCK mytable. Most other RDBMS's will have similar, but not necessarily identical language to effect the same behavior. This has the advantage that if your code fails unexpectedly, the database will remove the lock for you. You have to be careful if the resource (or one of the resources) you access is the database itself; in that case doing the rollback will not have the results you want ;)

All that said, the file locking is still the simpler and more straightforward solution in most (99.9999%) cases.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Replies are listed 'Best First'.
Re^3: Ways to sequence calls from multiple processes
by bcole23 (Scribe) on Nov 28, 2004 at 21:41 UTC
    If you want a lock in oracle, you're going about it the entirely wrong (though viable of course) way. Use dbms_lock.request example
    v_lock_sucess:=dbms_lock.request(lockhandle=>v_temp_lockhandle, lockmode=>dbms_lock.x_mode, TIMEOUT=>5, release_on_commit=>TRUE);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://410800]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found