Bruintje said:
Hi,
I have a server which has a class that updates a flat file. There are many
client who access the server. My question is how can I lock the data file so
that each client writes its data to the file without corrupting the data
from the other clients.
Make the clients pass the data to a single server instance as discrete
messages. Each message should contain a complete, self-consistent piece
of data. The server should queue input in arrival order. If the data
rate is high enough it can use several threads to accept messages from
clients. However, it must use a single thread to remove messages from
the front of the queue and write them to the file. This mechanism
guarantees that no corruption can occur and is OS agnostic (i.e.
unaffected by the locking mechanisms used by and OS.
I can see possible problems if the data value is high and if there's a
mismatch between the way data arrives at the clients and their ability
to pass it on in correctly assembled chunks. HOWEVER, until you tell us
about:
- the number of clients
- the data arrival pattern at a client
- the relationship between incoming data and how it is to be recorded
- the data arrival rate and volume
- if the total data arrival rate can exceed the file writing rate
- if the file is read-write or write-only.
its not possible to give more specific advice.
It doesn't matter whether the clients and server are written in COBOL, C
or some version of Java; the design constraints are the same.