wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes several questions on B&S assignment 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 "several questions on B&S assignment" Watch "several questions on B&S assignment" New topic
Author

several questions on B&S assignment

Shu Gao
Greenhorn

Joined: Aug 01, 2005
Posts: 18
Hi, everyone:

I'm new here, first, I have to say that I am really happy to find a forum like this, I've been struggled on the assignment for weeks, and I haven't started yet, there's just too many unclear questions, I thought I was hopeless, but not until I finally found this forum

I've been thinking about this B&S assignment on my own for weeks, and I still have the following 'silly' questions:

1) In the assignment specs, is 'locking' used to prohibit cocurrent access on the same record at the same time so as to protect data from corrupting? If so, I think I could simply have one instance of data file access class and synchronize the method that does file writing, so that data corrupting would never happen.. is this so? So what is this 'locking' really used for? or more specifically, when?

2) I've read other posts on this forum, in other people's opinion, 'locking' is used when you want to book/update a record, but right after booking, the record is unlocked, as Frans Janssen said:

client A: read record 1, seems to be not booked
client B: read record 1, seems to be not booked
client A: lock, update and unlock record 1: record 1 is now booked for A
client B: lock, update and unlock record 1: record 1 is now booked for B

so if B books the record right after A books it, what is the point of locking? Or does this locking should be persistent until the user determines to 'unlock' the record manually, but how does he do that or when does he do that? Is there a unlock button in the Client GUI?

3) unlock() method throws SecurityException whenever the lockCookie wasn't the original returned cookie by lock(), but when will this method ever throw this exception? Since each operations will try to gain the lock first, if he gets it, then no problem, if he doesn't, then he doesn't, in what occasion the user will unlock a already-locked record? As far as I am concerned, the user will always call lock() first, if it fails, it fails..as I mentioned above, is there a unlock button in the client GUI so that user could ever perform unlock before lock?

4) Does locking requries logic locking or physical locking, I was thinking about physical locking few days ago that I decided to use java.nio.channels FileLock that locks a portion of a file to accomplish this, what's everyone else's choice?

These are just few questions that are mostly unclear to me these days, hope I can find confidence here, Thank you very much, everyone!!

[ August 01, 2005: Message edited by: Shu Gao ]
[ August 01, 2005: Message edited by: Shu Gao ]

SCJP 1.4<br />SCJD<br />SCWCD 1.4
Frans Janssen
Ranch Hand

Joined: Dec 29, 2004
Posts: 357
2) I've read other posts on this forum, in other people's opinion, 'locking' is used when you want to book/update a record, but right after booking, the record is unlocked, as Frans Janssen said:

client A: read record 1, seems to be not booked
client B: read record 1, seems to be not booked
client A: lock, update and unlock record 1: record 1 is now booked for A
client B: lock, update and unlock record 1: record 1 is now booked for B

so if B books the record right after A books it, what is the point of locking?


Hi Shu,

The example you quote is to show what happens if you don't implement locking correctly. With proper locking, the scenario would be:

client A: lock record 1 -> lock granted
client B: lock record 1 -> already locked, B is put in wait
client A: read record 1, seems to be not booked
client A: update record 1 -> record 1 is now booked for A
client A: unlock record 1 -> B is now granted lock
client B: read record 1, seems to be already booked -> B aborts booking
client B: unlock record 1

So you see that locking is used to prevent that two clients book the same record, because they both think it is available. It is still the responsibility of the clients to abort booking when a record is already booked. (The locking mechanism would not stop B from overwriting the booking if B really persisted.)

Frans.
[ August 02, 2005: Message edited by: Frans Janssen ]

SCJP 1.4, SCJD
Samuel Pessorrusso
Ranch Hand

Joined: Jul 21, 2005
Posts: 164
You can not use any class of nio package. Read your assingment again.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Originally posted by Samuel Pessorrusso:
You can not use any class of nio package. Read your assingment again.


You must conform to the instructions you have been provided by Sun, however you may find that your instructions do not specify anything at all regarding NIO. You might like to look at this post for more information.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Shu Gao
Greenhorn

Joined: Aug 01, 2005
Posts: 18
Originally posted by Andrew Monkhouse:


You must conform to the instructions you have been provided by Sun, however you may find that your instructions do not specify anything at all regarding NIO. You might like to look at this post for more information.

Regards, Andrew


Thank you, Andrew and Samuel

I did find on the website that Sun stated java.io should be used, but not nio; however, in my assignment, I didn't even find the word nio, so I guess it is absolutely ok to use it

But I think nio would make the scenario more sophisticated, on the other hand, a proper logic locking mechanism would do the trick, right?
Shu Gao
Greenhorn

Joined: Aug 01, 2005
Posts: 18
Frans, Thanks a lot

Your point is very clear to me!!

Since I really don't have any solid experience on database itself, especially transaction-based system, I have no idea how it works, I've really learned a lot from your reply

By the way, can you still tell me under which circumstances that unlock() will throw that SecurityException? I don't think it is possible that unlock() even have a chance to throw it; since lock() will be called first, if succeeded, it will get the right cookie, but if not, it will give up..To be more specific, under what condition that unlock() will be called alone without lock() being called first in the same method like update() or delete()?

Finally, the specs stated that
If the specified record is already locked by a different client, the current thread gives up the CPU and consumes no CPU cycles until the record is unlocked

My question is that "give up the CPU and consumes no CPU cycles", does it mean the thread gives up totally? I was thinking about wait() at first, wait() puts a thread into a blocked state, but I wasn't too sure that if a blocked thread won't consume any CPU cycles at all...

Thanks again, everyone!

Regards

[ August 02, 2005: Message edited by: Shu Gao ]
[ August 02, 2005: Message edited by: Shu Gao ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Shu,
Originally posted by Shu Gao:
Does locking requries logic locking or physical locking, I was thinking about physical locking few days ago that I decided to use java.nio.channels FileLock that locks a portion of a file to accomplish this, what's everyone else's choice?
I think most people are only doing logical locking - the scenario suggested by Frans can be handled without resorting to physical locking.

From the FileLock API:
Whether or not a lock actually prevents another program from accessing the content of the locked region is system-dependent and therefore unspecified.
In other words, if you rely on this class for your locking, you run the risk of having you locking fail on the assessor's computer if they are running on a different operating system (very very minor risk).

Originally posted by Shu Gao:
can you still tell me under which circumstances that unlock() will throw that SecurityException?
Consider the following code that might be written by some junior / bad programmer:If you did not check the cookie then this code would work even if some other client owned the lock. You should defensively program your Data class so that such a scenario will not only fail but will throw the SecurityException.

Originally posted by Shu Gao:
My question is that "give up the CPU and consumes no CPU cycles", does it mean the thread gives up totally? I was thinking about wait() at first
wait() will work fine - most people use this

Regards, Andrew
[ August 02, 2005: Message edited by: Andrew Monkhouse ]
Shu Gao
Greenhorn

Joined: Aug 01, 2005
Posts: 18
Originally posted by Andrew Monkhouse:
Hi Shu, If you did not check the cookie then this code would work even if some other client owned the lock. You should defensively program your Data class so that such a scenario will not only fail but will throw the SecurityException.

wait() will work fine - most people use this

Regards, Andrew

[ August 02, 2005: Message edited by: Andrew Monkhouse ]


Thanks, Andrew, I see what u mean

really appreciate your help
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: several questions on B&S assignment
 
Similar Threads
must lock wait?
Multi-threading programming: we do not need lock() unlock() at all
NX Contractors 2.1.1: Lock persistence
Locking & Exceptions (B&S)
GUI display & booking questions