• 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

FBNS: Locking records and whole DB

 
Ranch Hand
Posts: 111
PHP Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all !
I am new on this forum and just started working on the exam. The implementation of the locking mechanism still gives some problems...
I follow the approach of the method described in the Habibi book. My implementation thus far is as follows:



First, I use a Set instead of a List as they do in the book. IMHO, we deal with an unordered collection without duplicates, so a Set is the better option. Or do I still miss anything ???
Second, I still have no implementation of "locking the whole database" when the value of -1 is passed.
What is a good way to implement this ? The lockedRecords variable only contains record numbers. Filling it up with all record numbers present in the DB is possible, but maybe a more elegant option is available.

Anybody has some ideas ??
TIA, Klaas
<Phil: enclosed the code above in a block to make it more readable/>
[ July 08, 2004: Message edited by: Philippe Maquet ]
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Klaas,

Welcome to JavaRanch and this forum!

You did great by starting your SCJD assignment with reading Max's book ... it's simply the greatest book on the topic, AFAIK.

First, I use a Set instead of a List as they do in the book. IMHO, we deal with an unordered collection without duplicates, so a Set is the better option. Or do I still miss anything ???



A Set looks better than a List, indeed. Now a Map could even be better in case you'd need to associate some data to each lock.

Second, I still have no implementation of "locking the whole database" when the value of -1 is passed.
What is a good way to implement this ? The lockedRecords variable only contains record numbers. Filling it up with all record numbers present in the DB is possible, but maybe a more elegant option is available.



Do you need it? It really depends on your instructions. If you do, I guess that "-1" could play the role of a special record number, in which case (and because it's special), the only thing you'd need to do is to adapt your waiting condition.

One question: do you think that the call to notifyAll() is useful? And if "yes", how would you justify it?

Regards,

Phil.
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Klaas
Welcome
You are in good hands now(Phil). So not much to say.



......
One question: do you think that the call to notifyAll() is useful? And if "yes", how would you justify it?

Regards,

Phil.



I think Phil is referring to notifyAll() in lock method. Good Luck.
 
Klaas van Gelder
Ranch Hand
Posts: 111
PHP Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx guys, for your very fast reactions !
I think your have a point Phil.
In the lock method, on my second thought, there indeed appears to be no reason to notify the other threads waiting for access to the record. Only in case of releasing the lock (so in the unlock method) this is necessary.
But Habibi tells a different story ! In his eample, notifyAll IS called in the lock method. His explanation: "Line 9 notifies waiting threads that reservedDVDs is free, and the (lock) method returns".
I do not understand this fully. In my opinion, the static Set (or List) variabele (locked on entering the synchronized(reeservedDVDs) block) is free and available when the synchronized block is completed.
The effect of a call to notifyAll on this variable results in notifying all threads waiting that an event has been occurred in their advance, so they need to become Runnable again.
But this is never the case when a record is locked. So thus far, I think that the notifyAll method has no effect in the lock method...
I look forward to your reaction ;-)


Furthermore, it is indeed possible to save some extra info with the locked record, thus using a Map instead of a Set. In some examples on this forum, they save the client ID and check this ID when trying to unlock the record. But is there ever a situation that a client tries to unlock a record which he did not lock ? Habibi says in his example that he ignores the very small chance that this happens...

And about locking the whole DB,
Indeed it is explicitly required to lock the whole DB when passing the value of -1, but the specs document does not tell when this should happen...

One last (?) issue, how to deal with the InterruptedException thrown by the wait method ? I guess that when the waiting thread is interrupted, there is no guarantee that the record is actually locked. So a DaabaseException can be thrown to the caller:



And in the calling code in the DataAdapter class:



Am I on the right way with this ? All suggestions are very welcome :-)

Greetz, Klaas
[ July 10, 2004: Message edited by: Klaas van Gelder ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Klaas,

Originally posted by Klaas van Gelder:
In the lock method, on my second thought, there indeed appears to be no reason to notify the other threads waiting [...]
But Habibi tells a different story ! In his eample, notifyAll IS called in the lock method.



Max could not make the solution in his book exactly the same as the solution you require for the assignment - to do so would lower the value of the assignment, and reduce the benefit and sense of achievement you will get when completing your assignment. So you will find that there are things that Max has done that you will need to reconsider before implementing. This concept of calling notifyAll() within the lock method is one of them.

I remember a long conversation with Max, Phil, myself, and Jim Yingst (and others) on the topic of why Max does call notifyAll(). From memory it was something specific to Max's assignment. I have a vague recollection that Max had a plan (possibly described in the book but not in code) to use this when locking the entire database - that way clients in his assignment could get notified that the entire database was locked.

Originally posted by Klaas van Gelder:
Furthermore, it is indeed possible to save some extra info with the locked record, thus using a Map instead of a Set. In some examples on this forum, they save the client ID and check this ID when trying to unlock the record. But is there ever a situation that a client tries to unlock a record which he did not lock ? Habibi says in his example that he ignores the very small chance that this happens...



Again, you have to be careful about which assignment you are working on: yours or Max's - You will only pass if you follow your instructions, and Max's assignment deliberately deviates from the real assignment (as does Alain Trottier's for those reading his book).

You must also be careful with the other threads in this forum - most people are currently working on the newer assignments which require candidates to track lock ownership in the data class, whereas FBNS only has the requirement "If an attempt is made to unlock a record that has not been locked by this connection, then no action is be taken.". This is slightly different and can mean that you can track lock ownership at a more logical point in your server


Originally posted by Klaas van Gelder:
And about locking the whole DB,
Indeed it is explicitly required to lock the whole DB when passing the value of -1, but the specs document does not tell when this should happen...



One possible case could be when you are shutting down the database - lock the entire database first, then no new locks can be granted.

There are other cases (e.g. database maintenance) that might benefit from locking the entire database, but in some respects what Sun intended for this requirement is irrelevant to us - the customer is asking for it, so therefore we must deliver it.

Locking each record in the database is time consuming and prone to being blocked as clients continue to lock and unlock the remaining records. You might want to consider using either a flag to indicate the entire database is locked, or locking the record "-1" - either may be faster / less problematic.

I think your solution for handling InterruptedException will work.

Regards, Andrew
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry guys for the delay in replying.

Satish, thank you for the correction (and the compliment ). Yes, I meant the notifyAll() in lock().

As far the latter is concerned, I've nothing to add to what Andrew wrote above, except that - from memory - Max agrees that in the context of our assignments (including those which require a database lock through the -1 special record number), there is no need for such a notification from the lock() method.

Klaas:
And about locking the whole DB,
Indeed it is explicitly required to lock the whole DB when passing the value of -1, but the specs document does not tell when this should happen...



You can think of everything written in instructions.html as being part of the specs. So *yes*, you got a version of the assignment which requires to implement locking of the whole db.

Regards,

Phil.
 
reply
    Bookmark Topic Watch Topic
  • New Topic