File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: Reading a deleted record - best way to handle this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B&S: Reading a deleted record - best way to handle this" Watch "B&S: Reading a deleted record - best way to handle this" New topic
Author

B&S: Reading a deleted record - best way to handle this

Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
My interface for read is


If a client calls the read method with a given recNo, the read method has to check whether the record is flagged as deleted. When encountering a deleted record, is it necessary to throw a RecordNotFoundException? e.g.



I'm in two minds about this. If my client tries to read a deleted record, some sort of exception from the data layer should be sent to the client, indicating that the record is deleted, hence a RecordNotFoundException.

On the other hand, my find method also calls the read method, and throwing the RecordNotFoundException to my find method forces the find method to handle the exception - in this case, the find method would just ignore the exception (ignore any deleted records). To me, this isn't good use of RecordNotFoundException.

Does anyone have any thoughts on proper ways it should be implemented?
Chih-Wei Lee
Ranch Hand

Joined: Feb 20, 2008
Posts: 129
Hi John:
I think you should adopt your first solution
cuz according to the instruction:
"Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file."
So if client read a deleted record, a RecordNotFoundException should be thrown.


SCEA, SCJD, SCDJWS, SCWCD, SCJP
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Hi Aaron,

A possible solution if you don't want to handle the RecordNotFoundException in your find method is to create a new private method for actually reading the record which doesn't throw an exception, this method would return null array if the record is not found or marked as deleted.



This new method would then be called by both the read and find, the read method would check if the returned value is null and throw a RecordNotFoundException, and the find method could ignore null array and not add to search results.

Hope it helps,
Jason
[ June 23, 2008: Message edited by: Jason Moors ]
Ronggen Liu
Ranch Hand

Joined: Jul 29, 2007
Posts: 40
Hi guys,

exactly, there is no deleted records in my application implementation, i didn't retrieve the deleted records when get the all records initially.the exception RecordNotFoundException in read method just will be throw out when there is no the record in my storage list, which holds all the records except deleted records.

Thank you!
Ronggen


SCJP 1.4,<br />SCJD Java 2,<br />...
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
Originally posted by Jason Moors:
Hi Aaron,

A possible solution if you don't want to handle the RecordNotFoundException in your find method is to create a new private method for actually reading the record which doesn't throw an exception, this method would return null array if the record is not found or marked as deleted.



This new method would then be called by both the read and find, the read method would check if the returned value is null and throw a RecordNotFoundException, and the find method could ignore null array and not add to search results.

Hope it helps,
Jason

[ June 23, 2008: Message edited by: Jason Moors ]


Okay, for example in the read method, the read method will need to read the actual database file, but these kind of operations may throw an IOException. How would you handle the IOException so that it doesn't propogate to calling methods?
Jo�o Batista
Ranch Hand

Joined: May 25, 2008
Posts: 40
Think about the kinds of Exception that a method can throw and how they affect the method signature.
You'll find a way to disguise that IOException.
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
Originally posted by Jo�o Batista:
Think about the kinds of Exception that a method can throw and how they affect the method signature.
You'll find a way to disguise that IOException.


Hi Joao,

I could not follow your previous post. Could you give me an example please?

Thanks.
Jethro Borsje
Ranch Hand

Joined: Jul 22, 2008
Posts: 100
Originally posted by Aaron John:


Okay, for example in the read method, the read method will need to read the actual database file, but these kind of operations may throw an IOException. How would you handle the IOException so that it doesn't propogate to calling methods?


You can also choose to read the data file into memory before hand, the Data class would then just hold a Map of the records in memory. Using this approach you do not have any IOExceptions during the usage of the Data class. You can catch these errors before hand we reading the file into memory. In my opinion this is also better, because you may not want th DB server to run at all if some serious IOExceptions occurred.


SCJP, SCJD
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
Originally posted by Jethro Borsje:


You can also choose to read the data file into memory before hand, the Data class would then just hold a Map of the records in memory. Using this approach you do not have any IOExceptions during the usage of the Data class. You can catch these errors before hand we reading the file into memory. In my opinion this is also better, because you may not want th DB server to run at all if some serious IOExceptions occurred.


Ah okay, that is a good approach. Thanks for that. How often would one read the database in order to have the latest records in memory?
Jethro Borsje
Ranch Hand

Joined: Jul 22, 2008
Posts: 100
Originally posted by Aaron John:


Ah okay, that is a good approach. Thanks for that. How often would one read the database in order to have the latest records in memory?

I believe you can assume that there is only one server which uses the data file at any time, so just reading the file in memory at startup of the server would be enough.

I have not implemented this approach entirely in my application, because I still have to find a way to make sure we save the data in memory back to the file. We should do this when the server is being shutdown, but we should also make sure we do not lose to much data when the server crashes. Perhaps saving the data file every x seconds / minutes is a good approach here?
Jo�o Batista
Ranch Hand

Joined: May 25, 2008
Posts: 40
Originally posted by Aaron John:


Hi Joao,

I could not follow your previous post. Could you give me an example please?

Thanks.


There are other threads mentioning this subject. A popular trend here is to use RuntimeException to deal with that error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: B&S: Reading a deleted record - best way to handle this