This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The locking in the create() operation

 
Joe Zhou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gurus,

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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is your logic for locking during create()?
 
Joe Zhou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. Locking is on record level and during a create there's no record (yet) so there's nothing to lock.
 
Joe Zhou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about two clients try to 'create' and both find the same deleted record
to place the data?
 
Sam Codean
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Zhou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the posts. Better understanding...
The synchronized mehtod may not block threads from calling on the
different objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic