Hi Zafer,
I am going to break your options differently before discussing them:
Having a Singleton Data class or not. Before implementing the Singleton
pattern,
you should ask yourself whether it is possible that
at any time in the future you might conceivably want to have more than one instance of the class. In this case, is there
any chance that someone might want to have more than one instance of the Data class in the future?
According to the current specifications, the answer might be no. However requirements change over time, and it is quite possible that at some point in the future there might be a need for the program to deal with additional data tables - as one example, they might want a table containing customer details (name, address, customer ids). There is nothing in the interface you have been given that is specific to the
type of data you are accessing - that is, the Data class
could be used for accessing a Customer table as well.
But here is the problem - if you have decided at this early stage that Data class is going to be a Singleton, then you
cannot have access both tables simultaneously. You are going to have the unpleasant choice of re-writting the Data class, or duplicating the Data class, or trying to code around it (which would end up with some potentially horrible code).
Having a Data class instance per connected client. The main advantages of this lie in the fact that (1) your Data class has less to worry about as far as
thread safety is concerned, and (2) the instance of the Data class itself can be used as an identifier.
Note that thread safety is still an issue, so you cannot rely on the first point too much. This is especially true when dealing with locking records - you must take care to ensure that this section of your code is thread safe. However this in itself could cause problems - you now have one part of your class that
must be thread safe, and another that is not so strict. This
could cause confusion for a junior programmer - make sure you add implementation documentation around the bits that are required to be thread safe.
The second issue only really affects those who (a) have lock methods without cookies and who are calling the lock methods from the client application using RMI, or (b) are using a WeakHashMap to clear stale locks (which we wont get into here). If you have a lock cookie, then you already have a method of identifying who owns the lock. If you are calling lock only from within a server side method or from a Sockets solution then you could use the thread as the lock owner identifier. But when using RMI to call the lock method without cookies, you need something else to identify the client - and having an instance of the Data class suits this admirably.
Having a single or multiple RAF instances At the end of the day, there is only one physical file on disk.
Some operating systems allow unrestricted access to the file (although this is rare), some only allow access to different disk blocks (at present, my Windows 2000 computer has a disk block size of 4096 bytes) within a file, and some only allow a single file pointer per physical file (although I suspect that
Java might abstract that so that it
appears that there are multiple file pointers per file - but I have not delved into this at all). So having multiple RAFs open
might give you a slight performance gain, but there are no guarantees.
[Side note: large database systems (such as Oracle) get around this by spreading tables over multiple physical files (preferrably on multiple hard drives), so no matter what the underlying OS does, the database can always give best concurrency.]
However there is another issue to be aware of: some operating systems (mainly the true multi-user systems) limit the number of open file pointers per process (I think Solaris has a limit of 32 file pointers per process). So if you allow every client to get their own file pointer, then you might be limited to only 32 clients
. The best way around this would be to have a pool of file pointers - but this means you are going to have to delve into pool management.
An alternative to consider might be whether a cache of data might work better than multiple RAFs. You would have to consider expected usage patterns for the file before you could consider this. And this is a separate topic in it's own right
.
This has just been a high level overview of some of the issues involved. It may have answered your question, and it may have raised new questions
. There is no one right answer (although you may have gathered that there are some that I consider wrong). You will have to weigh up the options yourself and choose for yourself what works for you (and what you are willing to put in your choices document and pottentially describe in your written exam).
Regards, Andrew