• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[B&S] how to check the records

 
Jimmy Chen
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know many of you use a ArrayList to store the locked record numbers.
As we need to throw RecordNotFoundException in many methods, I'm wondering
how to check the available records. I'm thinking about to use another
ArrayList to store all the record numbers, then I can check it in each method.

Another question, do I need to check if a record is in the locked-record Arraylist in "lock" and "unlock" method? Because there already has an "isLocked" method, I can call it before I call "lock" or "unlock".


public int [] find(String [] criteria)throws RecordNotFoundException;

public void lock(int recNo) throws RecordNotFoundException;

public void unlock(int recNo) throws RecordNotFoundException;

public boolean isLocked(int recNo)throws RecordNotFoundException;
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether you need to check if a record is locked whenever reading it depends on your database engine design.
If you require a lock to read you need to check that, if you don't you only need to check that when writing or deleting a record.

The RecordNotFoundException is thrown only when a record doesn't exist.
That means the record number is impossible (beyond the end of the table) or belongs to a record that was deleted.
Both can be determined on the fly as it were.
I've created a method to set the file pointer to the correct position based on the record ID. That method returns false if an attempt is made to position the file pointer beyond the last existing record, true if it was successfully positioned.
The calling method simply throws a RecordNotFoundException if that method returns false.
Then on read I check the delete flag and throw a RecordNotFoundException if it is set.

I've created a little helper method "exists(int recordId)" to encapsulate that behaviour, which returns false if either the record is beyond EOF or has the delete flag set.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic