Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whole Database lock question?

 
xiao gao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have no idea about the argument = -1 in the lock method. What "the entire database shoudl be locked" means? How to implement the whole data locked. I means what the purpose of the whole database locked? If lock(-1) and lock the whole database successfully, when and how to unlock the whole database?
I have code like this :
if (record == -1) {
isDatabaseLocked = true;
} else {
synchronized(getlocks()) {
while(isDatabaseLocked | | getlocks().contains(record)) {
try {
locks.wait();
} catch (InterruptedException e) {
throw new IOException("lock failed ");
}
}
getlocks().add(new Integer(record));
}
}
private Set getlocks() {
if(locks == null) {
locks = Collections.synchronizedSet(new HashSet());
}
return locks;
}
does my code make sense?
Regards
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>} catch (InterruptedException e) {
>>throw new IOException("lock failed ");
>>}
I'd remove the throw statement.
You want the lock method to be in a wait statement until the lock is released by the other client. The catch is there to release the wait state, and then you can continue on and lock the record. At that catch point, it is not a failed attempt, it is the go ahead to lock the record because the other client has released the lock, or possibly released it. If they release a different record, then the while loop puts the thread into wait state again, to wait for the record to be unlocked.
For the lock entire database, try recursion in the first part of your if statement. You will need to loop through all the record in the database.
Hope that helps.
Mark
 
xiao gao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mark,
Thanks for the reply but I still don't quite understand your word "For the lock entire database, try recursion in the first part of your if statement. You will need to loop through all the record in the database." Can you explain it to me detailly?
Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as best as I can without giving you the entire code. But Recursion is when a method calls itself.
So if I had 10 records 1-10
If I called the method with a -1
I would loop through 1-10 calling the lock method with the current loop value.
Mark
 
xiao gao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
You see, I have a boolean variable called isDatabaseLocked, if the record is -1, I will set that variable true, each time, a thread calls lock method, it will check this boolean variable first, then check if record number is in the locks collection. I think I use another approach to solve the whole databse lock, am I right?
Regards
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think the purpose of a Database lock is to keep all records locked, for maintenance, or simply closing the database.
Your solution of a boolean variable only states that someone has a database lock, but not that that someone actually locked every record.
Meaning. Client A, B, and C, all lock different records. Then Client D comes along a tries to get a database lock. By your solution, you just change the boolean property, and client D has the database lock, but client A, B, and C still have records locked. Then client D closes the database on A, B, and C before they even get a chance to unlocked their records. I believe that's a recipe for disaster.
Mark
 
xiao gao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Thanks for the reply, You are right.
But in my case, I register a RemoteData obejct to the RMI registry, and wrap the Data object inside of RemoteData. In the RemoteData, I keep a singlton Data object and the isDatabaseLocked is a static member level variable. So I think only one isDatabaselock available during the Data instance life time.
Any client will only get the same isDatabaseLocked varaible in my occasion,
Does my words make sense?
Reagrds
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you are saying. But again, I will say if one client has the database lock, no other users/client can have a lock on any record, before the database lock occured and after it occured.
I think in your solution, you leave open that possibility. I don't think you need the static variable. You just need to have the client lock each and every single record. So if client A locks record #5 and client B locks record #7. Client C needs to start locking each record, when it gets to #5 and #7 it needs to wait till those clients release those locks. Then it can continue on, lock record #5 and #7 and all the rest of the records.
Mark
 
xiao gao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mark,
I am fully clear now, thanks you great explaination.
!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic