views:

73

answers:

1

Hello,

I am using the java properties file construct. At the beginning I read it to populate a dialog, but I also give the users the ability to change the values in the dialog and click save. This causes me to call the setProperty method of the properties file. Now, since this webapp can exist over multiple browsers, all changing the same file, I want to be able to "lock" the properties file whenever I am in the "save" method. How can I accomplish this? I've seen similar questions refering to FileLock, but I am unsure if this applies to the properties file construct. Is each "setProperty" a different write?

Thanks!

A: 

setProperty merely updates the set of properties, it does not write the file. You have to call the store(OutputStream out, String header) method to actually write out the file to disk, and that is the time where you'll have to "lock" the file.

You can use FileLock to prevent multiple users from writing to the file at the same time, however, each time your user wants to save, you will need to reload the property file from disk, change the property, and write the file back again, all without releasing the file lock, to make sure no stale data is saved.

Nader Shirazie
I am unclear what you mean by this: "however, each time your user wants to save, you will need to reload the property file from disk, change the property, and write the file back again, all without releasing the file lock, to make sure no stale data is saved."
NewImageUser
If two users modify [different] properties at the same time, and the first user saves its changes, the second user is not going to get the new properties. So when the second user saves the file, the previously saved changes are discarded (overwritten). To fix this, the user has to reload the file, apply the changes made, and save the file.
Lajnold
So in order to "Write" to the properties file, I would have to LOCK then load the properties file, then do all of my setProperty then do my store(), then RELEASE. Correct?
NewImageUser
that is correct.
Nader Shirazie