My question is: can we do create-locking the same way as update and delete?
Will the following algorithm work:
creat(String[] data): 1. Search the file for a deleted record or a record at the end of the file. 2. Lock the record. 3. Write the data. 4. Unlock the record.
My studies show that the above algorithm runs into race-condition. Not sure if I am right. I don't hear any talking on this topic. Any comment would be appreciated.
Thanks,
Joe
Mike Ngo
Ranch Hand
Joined: Oct 16, 2006
Posts: 89
posted
0
what is your logic for locking during create()?
Joe Zhou
Ranch Hand
Joined: Oct 18, 2006
Posts: 34
posted
0
The same one as for update and delete as many gurus talked.
synchronized(lockObj){ while(isLocked(recNo)){ lockObj.wait(); } lock the record; }
Mike Ngo
Ranch Hand
Joined: Oct 16, 2006
Posts: 89
posted
0
when you update/delete records those are existing records. Since many clients may try to access the same one at the same time, they need to be locked. Create() insert new record so there is no locking involved.
Jeroen T Wenting
Ranch Hand
Joined: Apr 21, 2006
Posts: 1847
posted
0
Correct. Locking is on record level and during a create there's no record (yet) so there's nothing to lock.
42
Joe Zhou
Ranch Hand
Joined: Oct 18, 2006
Posts: 34
posted
0
How about two clients try to 'create' and both find the same deleted record to place the data?
Sam Codean
Ranch Hand
Joined: Feb 26, 2006
Posts: 194
posted
0
i synchronzize the CRUD methods so that at a time only one operation is performed. You cannot allow Read and write simultaneously to the file so this solves the issue of two creates finding the same record number to be created