aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Database Lock(-1)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Database Lock(-1)?" Watch "Database Lock(-1)?" New topic
Author

Database Lock(-1)?

Gosling Gong
Ranch Hand

Joined: Jun 20, 2002
Posts: 51
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?
Ian B Anderson
Ranch Hand

Joined: Jun 26, 2001
Posts: 275
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
Gosling Gong
Ranch Hand

Joined: Jun 20, 2002
Posts: 51
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!
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

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.
Ian B Anderson
Ranch Hand

Joined: Jun 26, 2001
Posts: 275
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
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
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 :-)
Gosling Gong
Ranch Hand

Joined: Jun 20, 2002
Posts: 51
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!
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
..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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database Lock(-1)?