I am waiting for answer for this post and no one yet replied.
I use the singleton design pattern to implement the remote object
originally, I have two choices to implement the synchronize mechanism. One, synchronize all methods of Data class, which are related to database operations. Two, just synchronize the RandomAccessFile object.
I think it probably produce performandce problem to respond quickly to client's request.
What I want is all the client threads can read db file simultaniously as long as no client thread performe write actions. Of course, if a client is perform write action in db file, all the other clients have to wait.
Do you actually force the RemoteDataImpl to be a Singleton, or do you simply create a single instance of it?
I would advise against forcing a Singleton - for what you want the same results can be achieved by only instantiating a single instance of the class. And you have no way of knowing whether sometime in the future someone may want to have multiple instances of that class.
Either would work in your situation, however I would recommend that you synchronize at the lowest level that is possible.
You could simply accept that this is going to happen: document it, and don't spend too much time on it
Or you could try having multiple instances of your random access file - then only synchronize on the actual read or write (this can lead to other issues though).
Or you could store all the records in a cache - this is probably going to give you the best performance of the lot.