Hi,
As mentioned previously, Data which implements DBMain
is considered relatively "low-level" in my design in
that there will be another class, let's call it
DataController, which exclusively will use Data; all
other components of the system that wish to read or
update the database file must do so by communicating
with DataController. DataController is a singleton
which itself holds a reference to Data which, through
the use of other classes refers to one RandomAccessFile.
Data will not have its methods synchronized nor will it
contain any synchronization of any kind. However,
DataController will use synchronization.
Data's methods are considered, for the sake of my question,
low-level methods which I will call "elemental methods."
[This is not strictly true, but let's just assume that the
methods are elemental and pretend that they only issue one
read or one write, and that's all. For, my question is
conceptual, and not implementation based, and I want to
simplify my question.]
DataController's methods are higher level, and one method
represents some atomic action, such as "book" which
would be represented in a dataController.book() method.
Usually, DataController's methods are compound, and consist
of a set of invocations upon Data's elemental methods.
Here is an example of dataController.book() method:
1. Using data.isLocked(), check that the record is not locked.
Wait until it is not locked and then lock it.
2. Using data.read(), check that the record has not been booked.
3. If the record has not been booked, book it using data.update().
4. Unlock the record using data.unlock().
Here is the type of data corruption I am trying to guard against
when multiple threads are "simultaneously" running:
1. I do not want a record to be read if another
thread could be
writing to that same record.
2. I do not want a record written if another thread is writing
that same record.
In other words, it looks like I am going to want both reads and writes
to be controlled.
Question: so, my first question is, is this normal? That is, _everything_
is controlled, and I will not allow, for instance, five reads to
proceed simultaneously. The reason I would not allow five reads to
occur "simultaneously" between five threads is that there is only
one file position pointer, so literally, everything, both reads
and writes need to be controlled.
Question: My next question is that given how I have designed things
so far, controlling the situation seems almost trivial; every
compound operation within DataController (assuming that I don't
get clumsly and dead-lock myself within DataController) simply
needs to be synchronized, and my data will never be corrupted.
So, since this seems trivial, I wonder if it is what Sun is
looking for; I'm pretty sure that it is logically correct,
and it's not really "trivial", but conceptually, disregarding
the devil in the details, it certainly is straightforward.
Finally, my simple, conceptual questions do not deal with other
complications in the assignment such as the following, so don't
consider these extra complications as a part of my conceptual
questions:
1. associating a record lock with a particular client,
2. what to do if a client shuts down prior to receiving
a response from the server,
3. what to do when a thread unexpectedly dies when it has
a record locked,
4. and so forth.
Thanks,
Javini Javono