• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLyBird: Question about find.

 
Jonny Butamer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been reading this forum for the last few months but this is my first time posting here. I have a problem and I� am hoping someone can give me some advice.

For the database part of my project I have a DataAdapter and a Data class. The DataAdapter does the locking and unlocking for clients and has a reference to a data object for accessing the database file.

The DataAdapter has a method called getRecordsUsingCriteria() that returns a list of records matching a supplied criteria. getRecordsUsingCriteria() calls the find() method in Data to get an array of record numbers that matches the criteria. Using the record numbers, getRecordsUsingCriteria() reads each record and adds it to a list which is returned to the client.

The find method in Data locks the file while it is searching record numbers. However the getRecordsUsingCriteria() does no locking. I think there is a possibility of a dirty read in the getRecordsUsingCriteria() method, if in between the time getRecordsUsingCriteria() receives the array of records and reads the records another client cuts in an changes the records.

I was wondering what is the best way to avoid this situation. I could either make getRecordsUsingCriteria() synchronized or call synchronized on the Data object and hold the lock for the duration of the method. Which one would be the best solution and would it work?

Any advice would be appreciated
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jonny,

There are several ways to handle dirty reads like you described.

You could synchronize on the Data object, but this would seriously cut down on your concurrency. I would not recommend this solution.

You could also implement some type of read lock functionality. This is probably outside the scope of the assignment, although I have heard of several people doing it.

You could do secondary checking in your DataAdapter class on the record numbers you get back from the Data object. I think this is a pretty common approach. It also helps with the problem of the "exact match" vs "starts with" discrepancy between the interface provided and the GUI instructions. Your secondary search would have to check each record to make sure it has not been deleted or changed in such a way to make the match invalid. The drawback to this is that there is a performance hit when using the search function.

Whatever you decide, make sure you document it in your choices.txt. Also, just a side note, it sounds like your DataAdapter class is actually implementing more of a Facade pattern than Adapter..
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonny,

As previously requested, could you please change your display name to comply with The JavaRanch Naming Policy..

Thanks,

Andrew
 
Jonny Butamer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Paul.

Why would locking the data object cut down on my concurrency? I tried it out and it doesn't seem to have any affect.

Thanks in advance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic