Hi webmasters. I already know how to use flock() but
I will deeply appreciate your comments and experiences on how much trust you put on this function before implementing a project. I've created online apps in Perl using <flat-files-databases> with no problem. Multiuser apps need file locking to avoid two people (or more) writing to the same file at once causing data loss or data corruption. Yes, I know databases do this but I'm still going to use Perl + flat files.
My way of implementing it: Whenever I'm using a file I first lock it, then read it-write to it and then unlock it. I made tests with flock(2) and if a user-process tries to read or write to the already locked file it will have to wait for the process to close-unlock the file before it can read it or write to it. This happens automatically without the need for the user to see any message and try again, the browser just waits milliseconds and the data is sent to it without any message or interruption, is transparent to the end user.
I had data loss and data corruption
before using flock,
not anymore so by now I'm happy with it.
Then why do I ask? I need your comments:
- Please share your general comments and experiences using flock on important data projects.
- How much do you trust flock(2) ?
- Would you use flock in the kind of project I'm working on?
- Have you experienced data loss even using flock() ? **
* Using flock to lock files involves using it every time you access the file in every routine. In my experience and tests, also for read and write. Please correct me if I'm wrong.
The situation: a table of widgets (flat file database of 2,000 aprox lines with 30 fields) that will be read and updated many times a day by many online users. The thing is, the kind of data has me kinda worried so this is why I ask. For my peace of mind, I've solved this before using one flat file for each record (2,000 files) so, the chances of accessing the same file at once are really less than putting all the data on the same file but I don't want to grow the project to so many data files.
I could do it again if there's no other way (many files) but using a single file would be more appropriate and convenient for this project (I don't know how less secure it would be in terms of flock). That's why I will need your comments, the kind of data is related to money.
Thanks in advance.