Forums Register Login

Database Lock(-1)?

+Pie Number of slices to send: Send
assume such situation:
connection 1: lock(1)
connection 2: lock(-1)
connection 3: lock(2)
should the lock(-1) wait untill all of the record unlocked? if so, then before the lock(2) unlocked, another record lock comes in, which will make lock(-1) wait endlessly?
+Pie Number of slices to send: Send
Hello Gosling,
When I did this I found the best thing was to write down the pseudo code:
1) Say record 2 is locked
2) Someone calls lock entire db -1
3) Someone calls lock record 3
You basically need a flag to say TRYING_ENTIRE_DB_LOCK and another for ENTIRE_DB_LOCK.
So when the lock entire db request comes in you set the TRYING_ENTIRE_DB_LOCK to true. When this is set you tell all new requests to wait(). So when the request to lock record 3 comes in it is simply told to wait().
Hope that helps
Ian
+Pie Number of slices to send: Send
Thanks, Ian!
Just confirm one thing, saying
1) Say record 2 is locked
2) Someone calls lock record 3
3) Someone calls lock entire db -1
4) Someone calls lock record 4
5) record2 unlock
when at 5) the record2 unlocked,only Database lock will be notified, others(including the one come in before the Database lock request) will still be waiting.
am I right?
thans again!
+Pie Number of slices to send: Send
 


You basically need a flag to say TRYING_ENTIRE_DB_LOCK and another for ENTIRE_DB_LOCK.


Actually, you only need one flag that you can set right after the call to lock(-1) enters the body of the method. Then all the subsequent lock() requests will wait() until 2 conditions are true:
1. flag is not set
2. the record is not locked
Eugene.
+Pie Number of slices to send: Send
Hello,
From memory you can't guarantee which thread will be notified so you have to call notifyAll() when you release a lock. This means you could build into your code something like:
The following is on a normal record lock:
wait.locks();
if (TRYING_ENTIRE_DB_LOCK) {
wait.locks();//wait again
} else {
//get lock
}
Ian
+Pie Number of slices to send: Send
I have it as simple as this (14 lines of code that cover both record locking and database locking). I consider this the best code I've ever written :-)
+Pie Number of slices to send: Send
Thanks, guys!
Eugene, I readed your code, but I am not sure how you release the Database Lock because you only have one flag databaseIsLocking, saying
1)client A lock record 1
2)client B lock database -1
3)client C lock database -1
4)client D lock record 2
5)client A unlock record 1
at this moment, I guess in your unlock, you simply call notifyAll(), how do you know how many client are waiting for Database locking so that you can set databaseIsLocking = false when all database locks have gone?
by the way, saying that one client want to lock two records at the same time,
1)client A lock record 1(assuming get lock successfully)
2)client A lock record 2(assuming waiting)
3)client B lock database -1
from you code, you will let client B lock the Database, but because the record locking is not empty, the client B has to wait for A release the lock, but A is also waiting for the lcok of record2(because the flag databaseIsLocking is true now), thus deadlock resulted.
thanks!
+Pie Number of slices to send: Send
..but I am not sure how you release the Database Lock because you only have one flag databaseIsLocking
There is no need to release the database lock. Once the database is locked, the server should go down. Also notice that unlock(-1) is not in the requirements.
you can set databaseIsLocking = false
Once the flag is set to "true", it never goes back to "false", -- you have to restart the server.

by the way, saying that one client want to lock two records at the same time,
1)client A lock record 1(assuming get lock successfully)
2)client A lock record 2(assuming waiting)

Right, my locking schema assumes that a client can only lock one record at a time.

Eugene.
[ July 16, 2002: Message edited by: Eugene Kononov ]
Seriously? That's what you're going with? I prefer this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 941 times.
Similar Threads
lock/unclock The correct question
lock() called twice by same client
Passed SCJD
Client Questions?
Is my locking mechanism going to work?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:19:36.