Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

file locking

Thread view: 
Bruintje Beer - 10 Jul 2007 19:52 GMT
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.

I use java 6

mark
Martin Gregorie - 10 Jul 2007 20:37 GMT
> 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.

> I use java 6

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.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Bjorn Borud - 10 Jul 2007 23:36 GMT
[Martin Gregorie <martin@see.sig.for.address>]

| its not possible to give more specific advice.

how about the lock() method in java.nio.channels.FileChannel?  I would
assume that this is implemented by means of flock(2) on most UNIX
systems and whatever underlying mechanism other operating systems
offer on other platforms?  If it does what I think it does, it should
be useable to do locking on files, then within the same JVM you would
need to use further concurrency mechanisms to ensure thread-safety?

-Bjørn
Martin Gregorie - 11 Jul 2007 13:59 GMT
> [Martin Gregorie <martin@see.sig.for.address>]
> |
[quoted text clipped - 6 lines]
> be useable to do locking on files, then within the same JVM you would
> need to use further concurrency mechanisms to ensure thread-safety?

I'd be hesitant to suggest using a locking mechanism without knowing a
lot more about the application. Locking may cause more problems for both
performance and program logic (dealing with lock collisions) than it
solves. Locking offers no help at all for data interleaving or strict
arrival order recording; the latter can be corrupted by lock collisions.

I suggested using a queuing mechanism that feeds a single writer process
because this approach side-steps all the above problems provided only
that it operates fast enough to keep up with the incoming data. However,
we don't know if this is a potential problem since the OP has provided
no information about message sizes and arrival rates.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Bjorn Borud - 11 Jul 2007 15:13 GMT
[Martin Gregorie <martin@see.sig.for.address>]

| I'd be hesitant to suggest using a locking mechanism without knowing a
| lot more about the application.

my response was more of a complement to what you had already answered.
(if people need to ask about these things you don't need to get your
hopes up anyway).

-Bjørn
Knute Johnson - 11 Jul 2007 03:35 GMT
> Hi,
>
[quoted text clipped - 6 lines]
>
> mark

How about synchronizing the writes?

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.