Hi ranchers, I recently completed the find method in Data. I show the whole code here, hoping your advices and rectifications . Some comments are as following: 1) my read() synchronize RAF and throw RecordNotFoundException with a String parameter "deletedRecord" in case of a deleted record and throw RecordNotFoundException with a parameter "overflowed" if the RAF's pointer out of the length of RAF. 2) i dont synchronize RAF in this method, cause the data will also be out of date even if i do such sychronizing. So i use a Collection to store the result of read(). Debate on improvement of arithmetic, brevity of coding, idea of designing are welcome and expected here.
You should never use exception description in program!!! 1) exception type -> for program 2) exception stacktrace -> for programmer 3) exception message -> for user
Better should be to made descendants of RecordNotFoundException.
Oh, now I see description is parameter, but generaly this so called "enumerated exceptions" are bad idea. I would prefer something like for(int i = 0; i < getRawRecordCount(); i++). Yes just after cycle ends the new record can be added, but this can happen in your code too. There is no chance (if you do not lock whole db for find) to avoid this - it is a real world [ April 05, 2006: Message edited by: Petr Hejl ]
Hi Zhixiong! Avoid inner exceptions is an extended good practice. Moreover if the exceptions are thrown inside a loop, and moreover if these exceptions are expected to occur frequently, as is your case. You can google something like "exception cost" java and find lots of explanations. Any way if you only are intested in performance, you can try something like this and extract your own conclusions:
After run your code, I can clearly see that Exception throwing is a slow procedure. Thanks for your good code.
So I did some modification to my find. 1) In Data I create a Vector variable deletedRecordsNo store deleted records' number. 2) add into delete method 3) remove and add before 'try'. But a disadvantage can be easily seen that if server crash, then the deleted records can not be recognized.Does such a disadvantage matter and influence the score?
In Data I create a Vector variable deletedRecordsNo store deleted records'
. IMO, that procedure adds complexity and potential problems for a doubtful benefit. Other case is implementing a full database cache, that obviously would provide a performance improvement, since, for example, you are reducing disk access. You can seach for "cache" in this forum. Regards