• 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

lock(-1)

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys/gals.
we have to implement lock(-1).
due we have to implement unlock(-1) also.
 
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't do it because it was not part of the spec.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Siddharth Mehrotra
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic