views:

4479

answers:

4

I have multiple applications running in one virtual machine. I have multiple virtual machines running on one server. And I have multiple servers. They all share a file using a shared folder on linux. The file is read and written by all applications. During the write process no application is allowed to read this file. The same for writing: If an application is reading the file no application is allowed to write it.

How do I manage to synchronize the applications so they will wait for the write process to finish before they read, and vice versa? (the applications inside a vm have to be synchronized and also applications across servers)

Curent implementation uses "file semaphores". If the file is about to be written the application tries to "acquire" the semaphore by creating an additional file (lets name it "file.semaphore") in the shared folder. If the "file.semaphore" file already exists this means the semaphore is already locked by a different application. This approach has the problem that I cannot make sure that the "file exists"-test and "create file"- operation are executed atomic. This way it is possible that two applications test for the "file.semaphore" file, see it does not exist and try to create the file at the same time.

+1  A: 

You can use NIO locking capabilities. See FileChannel#lock().

However, this will work only if underlying filesystem supports locking over the network. Recent NFS should support it. Probably, Samba supports them too, but can’t say for sure.

See article for example.

Ivan Dubrov
File locking helps for synchronizing between java vms. But not for applications inside one vm.
Eduard Wirch
A: 

Take a look at http://stackoverflow.com/questions/472329/how-can-i-synchronize-two-processes-accessing-a-file-on-a-nas#472365

Wolfwyrd
Thanks for your answer. I'm allready using file locking. But I'm not happy with this solutions. File locks tend to survive when processes crashes.
Eduard Wirch
A: 

Have a look at the Javadocs for the createNewFile() method - it specifically states that creating files is not a reliable method for synchronization, and recommends the FileLock class instead (it's another package in java.nio.channels so is essentially the same as what Ivan Dubrov is suggesting).

This would imply that your identification of the problem is accurate, and no amount of playing around will solve this with traditional file creation. My first thought was to check the return code from createNewFile(), but if the Javadocs say it's not suitable then it's time to move on.

Andrzej Doyle
I would prefer FileLock over creating files. But I have to synchronize threads inside one process too (inside different class loaders so the "synchronize" keyword wont work).
Eduard Wirch
A: 

I asked this questions a second time using a different subject: How to lock a file on different application levels?

Eduard Wirch