The requirements state that the create method:
"Creates a new record in the database (possibly reusing a
deleted entry)...."
So if possible a deleted record should be reused if one exists.
The need for synchronization here is best explained using an example
Say we have two clients accessing a remote db.
ClientA finds that record 2 is deleted and so concludes that they should
create their new record in place of this.
ClientA's thread loses the CPU and ClientB's thread gains the CPU.
ClientB deletes record 1.
According to the requirements ClientA should now be creating
the new record in place of record 1 and not record 2. To prevent this I
synchronized the create method.
Originally posted by Zhixiong Pan:
Hi Alen,
Could you please give out an example for such error?
Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available.
Originally posted by Oricio Ocle:
Ok think about that:
.... But it's record has not been unlocked ...
Regards
Originally posted by Alan Mc Kernan:
Hey Alan,
I don't quiet understand the solution presented. I also have a business layer encapsulating the 48-hour rule, and exact matches on searches etc.
How do you allow a client to book one record at a time? You mention "blocking".. do you mean that your book method is synchronized? I don't really understand why we need to do this. I can't see how any client could book more than one record at a time.
The test that john mentions, I dont understand this either. You create a Thread runner that will get a Service instance form the server and then try to book a specified record ? This thread runner will have a static field, which all of the threads will try to decrement? Or do you mean that you modify your book method to update a decrement a field (integer) in the database? And disable the 48 hour rule/allow the booking to be re-booked? And then fire the 50 threads?
I'd appreciate if you could ellaborate a bit.
Alan
Originally posted by Oricio Ocle:
Hello all
First of all, sorry for posting without reading the whole thread...
Alan, you are right:
But think what happens in notification...
What do your specs says about locking methods?
Your server must be capable of handling multiple concurrent requests, and as part of this capability, must provide locking functionality as specified in the interface provided above. You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server. Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available.