1

How can I aquire two shared FileLocks? I have the following test code:

File file = new File(lockDir, "tmp.lock");

file.createNewFile();
FileChannel channel = new RandomAccessFile(file, "r").getChannel();
boolean shared = true;
FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared);
assertTrue(lock1.isValid());
assertTrue(lock1.isShared());

// I get an OverlappingFileLockException here:
FileLock lock2 = channel.tryLock(0, Long.MAX_VALUE, shared);
assertTrue(lock2.isValid());
assertTrue(lock2.isShared());

lock1.release();
lock2.release();

But I get an OverlappingFileLockException in the marked line, even if I execute the lock-aquiring in two different threads. Or is a shared lock only possible for two different processes? Or did I misunderstood sharing and it is only about having write&read access for portions of a file ('sharing the file resource')? I cannot really find docs about whether my use case is possible or not.

I would like to have the same behaviour for the file based locking that one gets for new ReentrantReadWriteLock() with .readLock() and .writeLock() where multiple read-locks are allowed but only one write lock.

Karussell
  • 17,085
  • 16
  • 97
  • 197
  • What platform are you running on? From [the JavaDocs:](http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileLock.html) "Some platforms do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock." – Matt Ball Jun 23 '14 at 14:03
  • Ubuntu, but I cannot believe it has no support for it – Karussell Jun 23 '14 at 14:04
  • Indeed, if `assertTrue(lock1.isShared())` passes then it's probably not the issue. – Matt Ball Jun 23 '14 at 14:05
  • Yeah, I fear I misunderstood what 'share' means because locking different regions passes. – Karussell Jun 23 '14 at 14:08
  • 1
    I might be misunderstanding as well, because it sure sounds like your use case should work: _"A file lock is either exclusive or shared. A shared lock prevents other concurrently-running programs from acquiring an overlapping exclusive lock, but does allow them to acquire overlapping shared locks. An exclusive lock prevents other programs from acquiring an overlapping lock of either type."_ Perhaps it's a problem because you're trying to lock from within the same process. – Matt Ball Jun 23 '14 at 14:09
  • Thanks, I'll try that now! Because locking different regions also works for `shared == false`! – Karussell Jun 23 '14 at 14:12
  • You can acquire a `FileLock` for a given file region only once within your JVM. It’s not meant to be an intra-JVM concurrency tool. – Holger Jun 23 '14 at 14:18
  • Ok, indeed this works for different processes. – Karussell Jun 23 '14 at 14:19
  • @Holger yeah, that sounds reasonable but is ugly as I will need two different techniques to avoid writing to the same file. – Karussell Jun 23 '14 at 14:20

1 Answers1

0

from the java docs :

tryLock throws

OverlappingFileLockException - If a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping region

EDIT: I assume the condition met i your case is the overlapping region, since you only create the file, and it doesn't contain anything

omu_negru
  • 4,642
  • 4
  • 27
  • 38