Hi,
I am trying to use Java's FileLock class to synchronize writing to a
file **across machines**. The file resides on a file server. Under
Windows, it works perfectly (ie, only one machine at a time can gain
access, the others wait,no clobbered file, everything is written in
order). Under Linux (same code) the file gets clobbered. No exceptions
are thrown.
Can anyone offer suggestions??
Here is the critical code segment:
//lock the file and wait till we can
FileChannel channel = fos.getChannel();
FileLock lock = null;
try {
while ((lock = channel.tryLock()) == null) {
System.out.println (Utils.getHostname() + " Failed lock...wait");
Thread.sleep(100);
}
System.out.println (Utils.getHostname() + " Locked:" + lock);
System.out.println (Utils.getHostname() + " Lock type is "+
((lock.isShared())?"shared":"exclusive"));
System.out.println (Utils.getHostname() + " Is lock valid: " +
lock.isValid());
//write the title first if noone's done it
//and they asked for one
if (channel.size() <= 0 && bbTitle != null)
channel.write (bbTitle);
channel.write(bb);
} catch (Exception e) {
throw (e);
} finally {
if (lock != null) {
System.out.println (Utils.getHostname() + " Releasing lock");
lock.release();
}
}
Lew - 14 Jul 2007 22:28 GMT
> Hi,
>
[quoted text clipped - 39 lines]
> }
> }
This is the exact same code you posted the last time and a synonomous
question. I suspect the answers you got are still valid.
To recap, it seems the Linux locks are advisory and the Windows locks are
mandatory. Unless you control the processes that "clobber" your data, you're
not going to be able to enforce a lock. Are you able to write a "resource
manager" server to control the file, as was suggested before? Did you try
Patricia Shanahan's answer, and did it in any way affect the outcome?

Signature
Lew
alejandrina - 16 Jul 2007 15:25 GMT
> > Hi,
>
[quoted text clipped - 51 lines]
> --
> Lew
This thread was a re-post, because I never saw the orginal post until
much much later. Sorry for the confusion.
Martin Gregorie - 14 Jul 2007 23:24 GMT
> Hi,
>
[quoted text clipped - 6 lines]
>
> Can anyone offer suggestions??
If I understand your code correctly, you're trying to use a Lock object
/on the client computer/ to serialize access to a file /on a server/.
That isn't ever going to work because the other clients can't see each
other's Lock instances. I'd guess that the only reason it seems to be
working on the Windows server is because that box's internal filing
system serializes multiple access to a file for its own protection.
Linux, like all *nixen, has a filing system which, because it has no
internal reason to serialize file access, doesn't do it.
I think you have two possible workrounds:
1) Implement a central locking system on the file server.
2) Introduce a process on the file server to handle all access to your
file. There's already a thread on this subject: take a look.
You don't say what method you're using on the Linux file server to
provide access across the network. Is it Samba? nfs? afs? something else
like sshfs? Not all of them implement cross-network file locking, so
which one you're using has a bearing on how you implement central locking.
A discussion with your sysadmin followed by a bit of research and manual
reading should be helpful.

Signature
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
Lew - 15 Jul 2007 00:39 GMT
> 2) Introduce a process on the file server to handle all access to your
> file. There's already a thread on this subject: take a look.
That thread was started by the OP of this thread with the same question. One
hopes they've already taken a look.

Signature
Lew
Martin Gregorie - 15 Jul 2007 10:48 GMT
>> 2) Introduce a process on the file server to handle all access to your
>> file. There's already a thread on this subject: take a look.
>
> That thread was started by the OP of this thread with the same
> question. One hopes they've already taken a look.
Sorry - I missed that connection.

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