Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lock while reading data?

 
Mikey Chen
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I only add lock/unlock to those methods which change the record value. But it has a problem:
While read the record from db file, does it need to add a reading lock on record?
If there hasn't a reading lock. while one client thread is reading the record but another client thread changes the record. so the reading client maybe get two different results(before changed or after changed). I think this is not right. So, do I need to add lock to every method which accesses record (both read and write)?
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, do I need to add lock to every method which accesses record (both read and write)?

Here is your sequence: lock->read->modify->unlock
Eugene.
 
Jawad Kakar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do lock->read->modify->unlock when I try to book a flight. But when I show available flights and flights in combobox (which shows the origin and destination of flights) I do an optimist read (Reading without locking), any comment?
Jawad
 
Dan Collins
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not lock for performing a read to display the flights. What flights are displayed to the user are not necessarily garunteed to be available.
Locking for updating the display would significantly decrease the performance when there are multiple users.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic