0

I am using property file to store the count of two variables:

  • TransId
  • RegisNumber

The logic works as :
1) Initially the two variables is initialized to 1 and is stored in the Sequence.properties file.
2) The RegisNumber will be 1 whereas whenever call is made the previous value of TransId is fetched from the Sequence.properties file and is incremented to 1.

The requirement is: 'n' number of calls can be made simultaneously to the function executeRegNo(), so there is possibility of 'n' number of processes accessing the Sequence.properties file at the same time.

Modification done by me : I tried to put fileLock . The code is as follow,

public static String executeRegNo() {
    File file=new File("C:/Users/abc/Desktop/Files/GetCount.properties");
    Properties properties=new Properties();
    FileLock lock=null;
    if (!file.exists()) {
        try
        {
            file.createNewFile();
            properties.setProperty("TransNum", "0");
            properties.setProperty("RegId", "1");
            properties.store(new FileOutputStream(file), null);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    else   
        if (file.canRead()) {
            try
            {
                FileChannel fileChannel=new RandomAccessFile(file, "rw").getChannel(); // 1. modified
                lock=fileChannel.lock();//2. modified
                properties.load(new FileInputStream(file));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            String transId = properties.getProperty("TransNum");
            String RegisId = properties.getProperty("RegId");
            properties.setProperty("TransNum", String.valueOf(Integer.parseInt(transId) + 1));
            properties.setProperty("RegId", String.valueOf(Integer.parseInt(RegisId)));
            try
            {
                properties.store(new FileOutputStream(file), null);
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    String RId = properties.getProperty("RegId");
    String TId = properties.getProperty("TransNum");
    try {
        lock.release(); //3. modified
    } catch (IOException e) {

        e.printStackTrace();
    }
    DecimalFormat df = new DecimalFormat("00");
    String R = String.valueOf(df.format(Integer.parseInt(RId)));
    DecimalFormat df1 = new DecimalFormat("0000");
    String T = String.valueOf(df1.format(Integer.parseInt(TId)));
    return R + T;
}  

Error I'm getting is : The process cannot access the file because another process has locked a portion of the file.

Where to exactly put the FileLock in the code?

Kindly help in resolving the issues .

Thanks in advance.

Panther
  • 3,312
  • 9
  • 27
  • 50
  • Use synchronization instead. Create single syncronized method to update the file and call it from where you want to update count. Or you can use any other syncronization mechanism from java – Panther Jul 15 '15 at 03:58

2 Answers2

1

Where to exactly put the FileLock in the code?

Nowhere. File locks aren't a solution for thread safety. See the Javadoc.

You should be using synchronization for this.

Or a ReadWriteLock.

Or a database.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I used synchronized at the beginning of the method. and tried to access the same file from other program, but its locking the file. It is allowing the other program to access the file – Anjana suresh Jul 15 '15 at 04:02
  • You need to understand that `newFileOutputStream(...)` creates a *new* file, that isn't locked by this code. You're also leaking the `RandomAccessFile`. But this is never going to work if there are multiple processes. Use a database. – user207421 Jul 15 '15 at 04:14
  • Thanks.. How to go with the database? – Anjana suresh Jul 15 '15 at 04:30
0

Have a look at ReadWriteLocks, which is the optimal solution for files when you have multiple threads reading at the same time.

M. Shaw
  • 1,742
  • 11
  • 15