Re: Problem with FileLock
"alejandrina" <apattin@gmail.com> wrote in message
news:1184362454.730079.212490@n60g2000hse.googlegroups.com...
Hi experts,
We need to update a file (on a file server) from many different
machines. To synchromize the updates I am using FileLock.
Everything works as advertised (ie if a machine gets the lock,
it writes to the file while the other machines wait, everything is
written in the proper order). The same test, using Linux machines,
fails: the file is clobbered (meaning one update gets on top of
another). No Exceptions are thrown; in fact the debug statements
indicate that all the machines are acquiring the exclusive lock)
http://java.sun.com/javase/6/docs/api/java/nio/channels/FileLock.html
<quote>
Whether or not a lock actually prevents another program from accessing the
content of the locked region is system-dependent and therefore
unspecified. The native file-locking facilities of some systems are merely
advisory, meaning that programs must cooperatively observe a known locking
protocol in order to guarantee data integrity. On other systems native
file locks are mandatory, meaning that if one program locks a region of a
file then other programs are actually prevented from accessing that region
in a way that would violate the lock. On yet other systems, whether native
file locks are advisory or mandatory is configurable on a per-file basis.
To ensure consistent and correct behavior across platforms, it is strongly
recommended that the locks provided by this API be used as if they were
advisory locks.
[...]
On some systems, closing a channel releases all locks held by the Java
virtual machine on the underlying file regardless of whether the locks
were acquired via that channel or via another channel open on the same
file. It is strongly recommended that, within a program, a unique channel
be used to acquire all locks on any given file.
[...]
In general, great care should be taken when locking files that reside on
network filesystems.
</quote>
- Oliver