• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database Lock(-1)?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic