One way of protecting it would be be to make isValidRecord synchronized.
Originally posted by Leo Tien:
In my design, there are two reasons throws RNFE: 1. the record is invalid. 2. the record is deleted already. In the interface which sun give me named DBMain.java, the lock and unlock method must throw RNFE, so I have to access the database and I need to protect the raf access if it in unsynchronized context.
Protect the raf access, this is a big problem. At first, I use DataHelper deal with it. The design on DataHelper is shown on above, the first post. When one thread call the lock method in Data.java, it catch the Data's lock first, then call isValidRecord catch the DataHelper's lock to access the db file. Before it finish the db access, other thread cann't catch the DataHelper's lock any more, this isn't enough protect the raf access ???
I think use FileChannel is simpler than using synchronized RAF...
after isValidRecord is called and before synchronized(lockedRecords), there is a possibility that another thread using cpu cycle and delete this record and set the deleteflag to 1.