• 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

several questions on B&S assignment

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can not use any class of nio package. Read your assingment again.
 
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

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
 
Shu Gao
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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 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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic