JDK-4532474 : (fs) FileChannel.lock()/tryLock() methods unusable
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.0,1.4.0_02
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux,linux_redhat_7.2,windows_98
  • CPU: generic,x86
  • Submitted: 2001-11-28
  • Updated: 2002-03-28
  • Resolved: 2002-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.1 hopperFixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description

Name: jl125535			Date: 11/28/2001


java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)


1. While attempting to acquire a lock on a text file using the following code,
an unxexpected error message occurs:

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

class FileLockTest {

    public static void main(String args[]) throws Exception {
        RandomAccessFile raf = new RandomAccessFile("FileCopy.java", "rw");
        FileChannel channel = raf.getChannel();

        // FileLock lock = channel.lock();  // also fails with same message
        FileLock lock = channel.tryLock();

        System.out.println("File locked.  Press any key to continue...");
        System.in.read();

        lock.release();
        channel.close();
    }
}

3. The following error message is generated:
Exception in thread "main" java.io.IOException: The parameter is incorrect
        at sun.nio.ch.FileChannelImpl.lock0(Native Method)
        at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:515)
        at java.nio.channels.FileChannel.tryLock(FileChannel.java:912)
        at FileLockTest.main(FileLockTest.java:12)

4. This sample program works on Windows 2000 with both lock() and tryLock().

5. On Solaris 8, the sample program works with tryLock(), but lock() results
   in the following error message:
Exception in thread "main" java.io.IOException: No record locks available
        at sun.nio.ch.FileChannelImpl.lock0(Native Method)
        at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:477)
        at java.nio.channels.FileChannel.lock(FileChannel.java:812)
        at FileLockTest.main(FileLockTest.java:11)

6. If the behaviors in (3) and (5) are expected (see the customer workaround),
perhaps more appropriate error messages or a site for common compatibility
issues would help.  The current documentation

http://java.sun.com/j2se/1.4/docs/api/java/nio/channels/FileChannel.html#lock()

talks about platform dependencies.  Specific information for common platforms
would be a nice addition.

(Review ID: 135868) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic hopper FIXED IN: hopper INTEGRATED IN: hopper VERIFIED IN: hopper
14-06-2004

WORK AROUND Name: jl125535 Date: 11/28/2001 CUSTOMER WORKAROUND: Use the FileChannel.lock(long, long, boolean) or FileChannel.tryLock(long, long, boolean) methods. To lock the complete file in exclusive mode, use: FileLock lock = channel.lock(0, channel.size(), false); ======================================================================
11-06-2004

EVALUATION win95/98 has stricter limitations on the size of the locked segment and its offset. The lock/trylock methods do not work over NFS. ###@###.### 2002-02-28 While fixing this bug on Windows and Solaris we noticed that it also occurs on Linux, so we fixed that problem too. -- ###@###.### 2002/7/24
28-02-2002