I have just written the findCriteria part of my Data class and was wondering if I should be locking the matching records until the search is complete. I ask because say I find a matching record and while I am searching the remianing records another thread comes along and deletes that record my matching record is no longer valid. Any input would be great.
posted 10 years ago
Generally, only methods that actually modify the data should be locking any records. One reason for that is that to find anything will generally mean you will have to loop through all the records. In effect, the find operation will likely be the slowest of all your DB operations (O(n)). Therefore, locking the records in your find operation would mean a significant bottleneck.
If another thread deletes the record while the find operation is retrieving the records, so be it. It just means your data will be a little stale. I don't think there's a problem with that. If for instance, you use the find method within your update method (to find the appropriate method to update) and the find method returns a record that has been deleted by another thread, that would just mean that your update method will have to throw the RecordNotFoundException. [ April 04, 2005: Message edited by: Yevgeniy Treyvus ]