• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about locking whole database

 
mike seluker
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm working on locking the whole database mechanism. And the functionality of this Database lock I thought is that when there is a request of recNo = -1(DB lock) I'll not allow any clients to further request a record level lock. But before issuing -1 lock, I'll chk if there are any existing client waiting to get record level lock. I'll check this using an arraylist in which cliendid's get added, just before they go to wait state. Once the number of clients waiting for record level lock reduce to zero , the -1 lock (already in wait state) will get the DB lock and will do necessary operation.
Here is lock method code:
lock(recNo, clientid) {
synchronized on Hashmap { // this haspmap will store recNo and ID
// It is different from Arraylist used
// for storing waiting client's ID.
check for -1 recNo existing in hashmap.
if yes return; // not allowing further rec level lock request
else
check for record number. if some other client is already processing this record i.e. while (hashMap.contains(recNo)) {
synchronized on arraylist{
add the clientid to arraylist only when recNo != -1
if (recNo == -1) {// only then goto this while loop
hashMap.put(recNo, clientId) ; // this will put -1 into hashmap which does not allow any further client to request for record level lock (see the first if condition)
while(arraylist.size > 0) {
arraylist.wait();// here the -1 lock will wait till
//all the existing clients waiting for rec level
//lock will finish
}//end of inner while
}//end of if (recNo == -1)
hashMap.wait();
//after the notifyAll from unlock, chk for the existence
// client in the arraylist and remove accordingly
if (clientid is in arraylist) {
remove from arraylist
arralist.notifyAll();
}
}//end of synch on arraylist
} //end of outer while

hashMap.put(recNo, clientId);
}//end of sync on hashMap
}//end of lock method

unlock(recNo, clientid) {
chk whether the same clients has locked the record
if yes {
synchronized on hashmap {
remove from hashmap
hashMap.notifyAll();
}
}//end of if
}end of unlock
Is this mechanism thread safe? I've tested it couple of times but just want to confirm? I'd glad if u guys can find out any flaws or any improvement.
Looking for ur comments ASAP..
mike
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about a little bit of recursion. That way you don't have to keep track of who has locks, and who is waiting.
It will wait just like the rest of the threads for a lock on a record that someone else has. Eventually it will get all the locks, and you will have a database lock.
Just an idea
Mark
 
mike seluker
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark can u please elaborate ur idea..how u have implemented it? i didn't get what u said.
I'm really having a hard time implementing -1 lock.. still working on it..
Originally posted by Mark Spritzler:
What about a little bit of recursion. That way you don't have to keep track of who has locks, and who is waiting.
It will wait just like the rest of the threads for a lock on a record that someone else has. Eventually it will get all the locks, and you will have a database lock.
Just an idea
Mark

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, without giving the full answer away. Recursion is when a method calls itself inside itself. For instance
public class Recursion{
Recursion(){
}
public void recursionMethod(int id){
if (id == -1){
for (int i=0; i < 10; i++){
recursionMethod(i);
}
}
}
}
Does that help.
Mark
 
Andre Mermegas
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking of doing something like
wait(10000); if its a full database lock
and there are current record locks.If the
records are'nt unlocked by then, there may
be a problem with the client who made the
original lock.
what ya think?
 
mike seluker
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andres
consider a situation where client A gets the lock on record R1. And just before reading/modifying that record, CPU switches the context and some other process gets the CPU. Now client A doesn't get the CPU for (10000) period u specify and according to your logic, you will unlock the record R1. Also let say client B was waiting for same record R1. And client B gets the record and in the middle of processing the record. At the same time client A gets the CPU, so it will now try to read/modify the record R1 (assuming it is still having the lock, which is not true). Won't this lead to corrupt data.
Please comment...
Mike
Originally posted by Andre Mermegas:
I was thinking of doing something like
wait(10000); if its a full database lock
and there are current record locks.If the
records are'nt unlocked by then, there may
be a problem with the client who made the
original lock.
what ya think?

 
Andre Mermegas
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, i was thinking about this myself,
maybe a possible solution would be to
add a check for isDatabaseLocked() before
proceding with any add/modify/delete operation
This way if the administrator? locks the database
Here is the sequence.
----------------------
1.If any record locks are open, wait a specified
length of time before assuming client error. e.g.wait(10000)
Else immediate lock
2.If a client who has a record lock, but between the time he
recieved the lock and the time he tries to modify/delete/add
there is a database lock, what happens is he loses the lock
but doesnt know it, so one adds a check for a database lock
before any modify/delete/add.
I keep a private boolean value in a LockManager class
that keeps track of database locks, and have public method
that returns the status.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic