• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX: File Synchronization #2

 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Javini,


Question: so, my first question is, is this normal?


YES. I implement the system in this way too.


I wonder if it is what Sun is looking for;


I do not sure whether Sun really look for this implementation. However, based on those people who passed the exam, they may either using "Caching" or "No Caching".
Both methods require synchronization well. But one thing that different is, for using caching, records can be read concurrently because there is no "file pointer" concepts. What did they usually do is, there is a DataManager which manages all data, and serve requests when needed.


2. what to do if a client shuts down prior to receiving
a response from the server,


RemoteException will be encountered, and I have just logged this down, without further action (since nothing to be displayed to the client if it shuts down).


3. what to do when a thread unexpectedly dies when it has
a record locked


For any operations, I will put them into a try-catch-finally block.
No matter what exception happened, if the operation will obtain a lock before accessing it, it will "finally" release the lock it holds.
Hope this help.
Nick.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic