Rod Sinclair wrote:
Client B reads record 2.
Client A deletes and updates record 2.
Client B books record 2.
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
Rod Sinclair wrote:
Client A locks record 2.
Client A deletes record 2.
Client A unlocks record 2.
Client A locks record 2.
Client A creates new record recNo 2 reused.
Client A unlocks record 2.
Client B now has stale data for record 2.
Client B locks record 2.
Client B books record 2, wrong hotel.
Client B unlocks record 2.
Rod
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
Rod Sinclair wrote:Oracle's specification states that "Your user interface should be designed with the expectation of future functionality enhancements...". These enhancements would obviously include delete and create. So you should not reuse record numbers.
Rod Sinclair wrote:If you reuse record numbers do you maintain a HashMap of record numbers and mark the ones that are deleted?
// Deletes a record, making the record number and associated disk
// storage available for reuse.
// Throws SecurityException if the record is locked with a cookie
// other than lockCookie.
public void deleteRecord(long recNo, long lockCookie)
Roel De Nijs wrote:
I'm very curious to see if you'll pass by implementing the methods which are not used by just throwing an UnsupportedOperationException
Rod Sinclair wrote:Roel - just because create/delete are not referenced in the project does not mean that you can reuse record numbers, plan for future functionality. You are not displaying impeccable logic.
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
instructions.html wrote:// Creates a new record in the database (possibly reusing a
// deleted entry). Inserts the given data, and returns the record
// number of the new record.
Your data access class must be called "Data.java", must be in a package called "suncertify.db", and must implement the following interface:
Rod Sinclair wrote:You are not displaying impeccable logic.
Roberto Perillo wrote:The thing is, you have to lock a record before reading it. Then you do what you have to do and unlock it.
Elchin Asgarli wrote:Is this a way or does it fall under category of "too complicated for junior programmer"?
Elchin Asgarli wrote:Is this true for all versions of assignment? Because my read function has no locks inside of it.
Rod Sinclair wrote:Client B locks record 2.
Client B reads record 2.
Client B unlocks record 2
Rod Sinclair wrote:If you reuse record numbers I cannot see how the above scenario can be avoided.
Roel De Nijs wrote:I guess Roberto was a bit too quick in his explanation, what he meant was. Before you book (update) a record, you'll lock it (now nothing else can happen with this record), then you can read the record (see if it is still available), update the record (with the customer id) and finally unlock the record.
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
Roel De Nijs wrote:
Elchin Asgarli wrote:Is this a way or does it fall under category of "too complicated for junior programmer"?
Sounds ok to me
Roel De Nijs wrote:
You don't have to lock a record for reading. I guess Roberto was a bit too quick in his explanation, what he meant was. Before you book (update) a record, you'll lock it (now nothing else can happen with this record), then you can read the record (see if it is still available), update the record (with the customer id) and finally unlock the record.
Elchin Asgarli wrote:So reading right before the update is for verifying whether the record is still the one we used to fetch?
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
Roberto Perillo wrote:
Elchin Asgarli wrote:So reading right before the update is for verifying whether the record is still the one we used to fetch?
Well, actually it is to make sure that the record hasn't been booked already or hasn't been deleted.
Rod Sinclair wrote:Roel - I lock a record in the read method before the calls to seek and readFully. This is because another thread could be updating the same record at the same time the read is taking place in which case read would return a corrupted record. I assume that you synchronize the entire read method to prevent this happening.
Rod Sinclair wrote:An optimistic locking mechanism sounds complicated. It is simpler to not reuse record numbers and Oracle say keep it simple.
Roel De Nijs wrote:I don't agree with Roberto (which is highly unusual ) When the record is deleted already, I would expect the lock method to throw the RecordNotFoundException. So when the lock succeeds, you have a valid (non-deleted) record and no other thread can modify or delete it.
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
Rod Sinclair wrote:Roel - are you sure that reading without any synchronization is thread-safe? There is a critical section of code in read.
Rod Sinclair wrote:here is the secret. I call lock and unlock inside the read method to lock the record, just around the critical section of code. You can use my read code without first locking and then unlocking. It is thread safe.
Rod Sinclair wrote:maximum concurrency
Norbert Lebenthal wrote:What's your pick on that ?
Roel De Nijs wrote:
Norbert Lebenthal wrote:What's your pick on that ?
You wrote a whole lot of documentation for just 1 single line of code. I used the same System.nanoTime() to get my client identification number and just added in the choices.txt that in the future this "algorithm" could get improved (make more unique) if necessary. That's it!
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
Roberto Perillo wrote:Today, I'd use the current Thread's id.
Roel De Nijs wrote:If you use a thin client approach, you can take that approach.
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|