Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton raf versus Multiple rafs

 
Don Wood
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have noticed that some people are doing a new RandomAccessFile(dbFile, "rw"); every time they access the file. Although it seems kind of high in overhead, they don't have to synchronize to keep the file pointer from moving. I believe this design does need to lock the record when reading as well as writing.
In thinking about the tradeoffs I realized that I am not clear whether or not RandomAccessFile can handle writing to different records in the same file from multiple threads unless the callers are synchronized. For example, if two adjacent records reside in the same physical disk buffer but are being written to by different unsynchronized threads through different rafs, will the file still be consistent?
Of course, we can synchronize the read and write methods after all but if we have to do that then what is the advantage of multiple rafs over a singleton?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Don,

I have noticed that some people are doing a new RandomAccessFile(dbFile, "rw"); every time they access the file. Although it seems kind of high in overhead, they don't have to synchronize to keep the file pointer from moving. I believe this design does need to lock the record when reading as well as writing.

Does this really the case?
If we do not make use of record lock, if 2 remote clients accessing the same record, and both of them want to book this record, what happened?
Even they are different records, consider:
RAF1 load the file, RAF2 also load the file. RAF1 writes X to the file, RAF2 writes Y to the file. Then both of them closed.
So, what will be the final look of the file? Does it contains X? or Y? or both?
We are not only need to make sure that the same record will not be updated for 2 processes, we also need to make sure that ONLY 1 write to the physical file, otherwise, the data will be in inconsistent state.

In thinking about the tradeoffs I realized that I am not clear whether or not RandomAccessFile can handle writing to different records in the same file from multiple threads unless the callers are synchronized. For example, if two adjacent records reside in the same physical disk buffer but are being written to by different unsynchronized threads through different rafs, will the file still be consistent?

Definitely NO.

Of course, we can synchronize the read and write methods after all but if we have to do that then what is the advantage of multiple rafs over a singleton?

Making use of a single RAF makes sure that, at any time, at most 1 request can operate with the database, and make no effort to handle multiple RAF synchronization.
Of course, if we really use a DBMS, we have a buffer cache, and we only touch the file when the data can be written to the disk.
But for physical file, we need to do what DBMS do to maintain the consistency of the file. Some ppl even make use of memory cache to load all data into the memory. But I havent done so in order to avoid the complexity of the system.
Nick.
 
Don Wood
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nicholas,
Thanks for your reply.
I have been planning to have a raf singleton but I wanted to understand why some people were choosing to have multiple instances. Because of these questions (and your answers), I am going to stay with the singleton. It is simpler to understand and I think more efficient.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic