Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock(-1) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock(-1)" Watch "lock(-1)" New topic
Author

lock(-1)

Siddharth Mehrotra
Ranch Hand

Joined: Aug 21, 2001
Posts: 185
hi guys/gals.
we have to implement lock(-1).
due we have to implement unlock(-1) also.


SCJP, SCJD.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
I wouldn't do it because it was not part of the spec.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Siddharth,
I'm allowing an unlock(-1). What if the lock(-1) is issued for temporary maintanence, like restoring the db to a known consistent state after some abnormal situation. Although that may not be technically a requirement for this project, it allows for future extensibility.
That's my opinion
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Siddharth Mehrotra
Ranch Hand

Joined: Aug 21, 2001
Posts: 185
hi Michael ,
that was precisely what i was thinking.
that when i give a lock command , which is mostly from a server , then there must be an unlock command also.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

The way I looked at it was that when the server is to be shutdown, we need to make sure no client user has a lock on any of the records. Therefore the server will issue the lock(-1) to get everyone out, and no longer allow locking by clients, then it can call close, and therefore the server and database are no longer running, so when would it call unlock(-1) when it is already shutdown.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Adam Caldwell
Greenhorn

Joined: Mar 27, 2002
Posts: 17
You won't loose points for implementing it. The way the instructions are written, unlock(id) unlocks the record specified by the id... Since id=-1 specifies the entire database, that could be seen as a requirement to implement it....
In my code, unlock(-1) only required a few extra lines of code, although I never called unlock(-1) from anywhere in the code.
-Adam
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
hi mark,
The way I looked at it was that when the server is to be shutdown, we need to make sure no client user has a lock on any of the records. Therefore the server will issue the lock(-1) to get everyone out, and no longer allow locking by clients, then it can call close, and therefore the server and database are no longer running,

i am using the rmi connection factroy design, and have currently coded the lock(-1) functionlity within my RemoteDataConnection class - becasue my interpretation of the requirments is that this option should be available via the data client, even though as you say (which makes total sense), lock(-1) would probably only be called by the dataserver, as part of it's shutdown sequence.
what i was wondering is, in your say DataServer.shutDownServer method, are you getting a RemoteDataConnection and calling lock(-1) or are you just calling Data.lock(-1).
By using the first option, I am holding a remote connection object in the server, which doesn't seem right.
By using the second option - the code to iterate through the database and apply a lock on each record is duplicated in both Data.lock and RemoteDataConnection.lock - which doesn't seem right either.
any advise - greatly appreciated.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
I called Data.lock(-1) during server shut down instead of using an instance of the connection object. Also I didn't iterate through all the records. I had a boolean flag to check for the lock on the entire database in Data.lock() method.
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
hi sai,
if your lock(-1) implementation didn't iterate through each record in the database, calling lock() for each recrod.
how did you guarentee that no clients had locked out any of the records, befroe shutting down the database ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

I think you have to iterate through all the records and lock each one, for that specific reason of making sure you have the entire lock, and wait for each individual client to finish up their work, and unlock their records. I think this is the cleanest and best way to make sure you do not corrupt the db.db file.
If you use Sai's version of a boolean, you can keep clients from getting new locks, but aren't 100% that the clients that already have locks are done with them when the server shuts down.
Mark
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
In my implementation,
A thread trying to lock a record is blocked if the database is locked or the record is already locked by some other thread.
A thread trying to lock the entire database is blocked if the database is locked or one or more records in the table is locked by some other thread.
Below is the while condition for wait() in the lock() method:
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

A thread trying to lock the entire database is blocked if the database is locked or one or more records in the table is locked by some other thread.

OK, so now the server is trying to get a lock on the database, client A has a lock on record 1, client B has a lock on record 2. the server wants to shutdown, it is blocked. Now client A unlocks record 1, while client C locks record 3, then Client B unlocks record 2, but in the meantime another client D locks record 4.
This could go on forever in your senario, Sai. There is no guarantee that the clients will ever completely have no locks on any record. That is why the server needs to go through and lock each and every record to be 100% sure it can shut down.
At the least, this is the quickest way a server can shutdown.
Mark
Eduard Jodas
Ranch Hand

Joined: May 14, 2002
Posts: 80
Hi,
I've followed Sai's solution. I find it better because:
- when client A is blocked wanting to lock all the database other clients can work. If iterating, the records already locked by A can not be used
- it is harder to fall into deadlocks: client A, performing a lock(-1), locks the first 10 records and blocks because record 11 is locked by client B, but client B blocks trying to lock record 1...
When a shutdown is started I don't allow any client to lock any record so the scenario described by Mark can never happen.
But Mark, what do you do if some client adds a record when another client has locked the whole database? In your solution, is there any difference between a client calling lock(-1) an a client iterating to lock all the records?
I also think there should be the possibility to force a shutdown in case a client never unlocks its records.
Ed
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Mark,
Locking every record is probably the quickest way depending on the volume of calls and the thread priority. It is like closing the tables in a restaurant before anyone can eat dinner! In my case, I wait for anybody to come in, eat dinner and close the restaurant when everyone is done with the dinner. I guess I can make more revenue
I guess the deadlock scenario mentioned by Ed in the prev post is a possibility in your design. Implementing any of the designs is probably easy. As long as the server doesn't allow anyone to access the Data instance during the shut down process, my implementation is also the quick way to do it.
[ May 27, 2002: Message edited by: Sai Prasad ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

because record 11 is locked by client B, but client B blocks trying to lock record 1...

This cannot happen, because of the lock-read-modify-unlock sequence. Client B therefore could not have lock on 11 while trying to also lock #1. no dealocks ever. But this is a contract that you agree upon.
Sai. Yes you will make more sales, but if it is slow and you have all your workers there, you are probably spending more money on the labor, and up-time.
Mark
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: lock(-1)
 
Similar Threads
Post Database lock scenario
lock()/unlock() again ...
Do we need to call add and delete method?
locking the entire database(-1)
How to Lock(-1)