OPERATING SYSTEM(S): SunOS 5.9 Generic_112233-11 sun4u sparc SUNW,Ultra-60
FULL JDK VERSION(S): 1.4.2_05-b04
DESCRIPTION:
We are having problems with the FileChannel exclusive lock support provided
by the New/IO in JDK 1.4. We have supplied a sample program below that
demos this problem. The sample will repeatedly attempt to obtain an
exclusive lock on a file and then, once it has gained it will pause for 30
seconds before releasing the lock again.
Run the testcase as
java Lock <filename>
Here is a testcase :
-----------------------------------------------------------------------
import java.io.File;
import java.io.NotSerializableException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
class Lock
{
public static final void main(String[] args)
{
try
{
System.out.println("Define File");
File file = new File(args[0]);
System.out.println("File defined");
System.out.println("Define RandomAccessFile");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
System.out.println("RandomAccessFile defined");
System.out.println("Get FileChannel from RandomAccessFile.");
FileChannel channel = raf.getChannel();
System.out.println("Got FileChannel");
FileLock lock = null;
int lockAttempt = 0;
while (lock == null)
{
lockAttempt++;
System.out.print("Attempting lock (cycle#"+lockAttempt+")
... ");
try
{
lock = channel.tryLock();
}
catch (java.io.IOException exc)
{
System.out.println("Unexpected IO Exception. No lock
granted " + exc);
}
catch (OverlappingFileLockException e)
{
System.out.println("No existing lock but we still go an
OverlappingFileLockException. No lock granted either");
}
if (lock != null)
{
System.out.println("Got the lock!");
}
else
{
System.out.println("No lock. Wait for 5 seconds and
retry.");
try
{
Thread.sleep(5000);
}
catch (Exception exc)
{
}
}
}
for (int sleep=5;sleep>0;sleep--)
{
System.out.println("T-" + sleep*5 + ". Sleeping for 5
seconds");
Thread.sleep(5000);
}
System.out.println("Unlocking");
lock.release();
System.out.println("UnLocked");
channel.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
When this program is executed concurrently on two separate machines to a
filename that resides on an NFS mounted disk it allocates ownership of the
"exclusive" lock to both process at the same time. You see the following
output on both processes.
Define File
File defined
Define RandomAccessFile
RandomAccessFile defined
Get FileChannel from RandomAccessFile.
Got FileChannel
Attempting lock (cycle#1) ... Got the lock!
T-25. Sleeping for 5 seconds
T-20. Sleeping for 5 seconds
T-15. Sleeping for 5 seconds
T-10. Sleeping for 5 seconds
T-5. Sleeping for 5 seconds
Unlocking
UnLocked
We should see lock contention on one of these two processes along these
lines:
Define File
File defined
Define RandomAccessFile
RandomAccessFile defined
Get FileChannel from RandomAccessFile.
Got FileChannel
Attempting lock (cycle#1) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#2) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#3) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#4) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#5) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#6) ... Got the lock!
T-25. Sleeping for 5 seconds
T-20. Sleeping for 5 seconds
T-15. Sleeping for 5 seconds
T-10. Sleeping for 5 seconds
T-5. Sleeping for 5 seconds
Unlocking
UnLocked
WebSphere recovery logic requires the ability to gain an exclusive lock
on a network file resource.
###@###.### 10/5/04 04:17 GMT
FULL JDK VERSION(S): 1.4.2_05-b04
DESCRIPTION:
We are having problems with the FileChannel exclusive lock support provided
by the New/IO in JDK 1.4. We have supplied a sample program below that
demos this problem. The sample will repeatedly attempt to obtain an
exclusive lock on a file and then, once it has gained it will pause for 30
seconds before releasing the lock again.
Run the testcase as
java Lock <filename>
Here is a testcase :
-----------------------------------------------------------------------
import java.io.File;
import java.io.NotSerializableException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
class Lock
{
public static final void main(String[] args)
{
try
{
System.out.println("Define File");
File file = new File(args[0]);
System.out.println("File defined");
System.out.println("Define RandomAccessFile");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
System.out.println("RandomAccessFile defined");
System.out.println("Get FileChannel from RandomAccessFile.");
FileChannel channel = raf.getChannel();
System.out.println("Got FileChannel");
FileLock lock = null;
int lockAttempt = 0;
while (lock == null)
{
lockAttempt++;
System.out.print("Attempting lock (cycle#"+lockAttempt+")
... ");
try
{
lock = channel.tryLock();
}
catch (java.io.IOException exc)
{
System.out.println("Unexpected IO Exception. No lock
granted " + exc);
}
catch (OverlappingFileLockException e)
{
System.out.println("No existing lock but we still go an
OverlappingFileLockException. No lock granted either");
}
if (lock != null)
{
System.out.println("Got the lock!");
}
else
{
System.out.println("No lock. Wait for 5 seconds and
retry.");
try
{
Thread.sleep(5000);
}
catch (Exception exc)
{
}
}
}
for (int sleep=5;sleep>0;sleep--)
{
System.out.println("T-" + sleep*5 + ". Sleeping for 5
seconds");
Thread.sleep(5000);
}
System.out.println("Unlocking");
lock.release();
System.out.println("UnLocked");
channel.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
When this program is executed concurrently on two separate machines to a
filename that resides on an NFS mounted disk it allocates ownership of the
"exclusive" lock to both process at the same time. You see the following
output on both processes.
Define File
File defined
Define RandomAccessFile
RandomAccessFile defined
Get FileChannel from RandomAccessFile.
Got FileChannel
Attempting lock (cycle#1) ... Got the lock!
T-25. Sleeping for 5 seconds
T-20. Sleeping for 5 seconds
T-15. Sleeping for 5 seconds
T-10. Sleeping for 5 seconds
T-5. Sleeping for 5 seconds
Unlocking
UnLocked
We should see lock contention on one of these two processes along these
lines:
Define File
File defined
Define RandomAccessFile
RandomAccessFile defined
Get FileChannel from RandomAccessFile.
Got FileChannel
Attempting lock (cycle#1) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#2) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#3) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#4) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#5) ... No lock. Wait for 5 seconds and retry.
Attempting lock (cycle#6) ... Got the lock!
T-25. Sleeping for 5 seconds
T-20. Sleeping for 5 seconds
T-15. Sleeping for 5 seconds
T-10. Sleeping for 5 seconds
T-5. Sleeping for 5 seconds
Unlocking
UnLocked
WebSphere recovery logic requires the ability to gain an exclusive lock
on a network file resource.
###@###.### 10/5/04 04:17 GMT