Jump to content

SimpleXML lock file


keben

Recommended Posts

Hey dudes,

 

I have an application that must access a jnlp file (xml, java web start), and change it with the choices of a user with simpleXML. The problem is that there's a probability that this happens with simultaneously users. How do i lock the file for the user that gets there in first place?

 

Cheers n Beers

Link to comment
Share on other sites

I grabbed this from the php documentation:

<?php

$fp = fopen("/tmp/lock.txt", "w+");

if (flock($fp, LOCK_EX)) { // do an exclusive lock
    fwrite($fp, "Write something here\n");
    flock($fp, LOCK_UN); // release the lock
} else {
    echo "Couldn't lock the file !";
}

fclose($fp);

?>

Link to comment
Share on other sites

Since simplexml_load_file() is read only, that shouldn't be a problem, should it? However, just for fun, supposing you still want to lock it, you don't have to lock the file itself. Let's say your file is whatever.xml. Then, if you want to be sure you are the only one reading whatever.xml (but you can't use fopen/flock), you can use fopen/flock on whatever.xml.lock. Essentially, you are creating your own lock system using filenames.

So, when you are about to read the xml file, you first attempt to fopen/flock the xml file with a ".lock" extension. If you can do that, then you go ahead. If not, then someone else has a lock.

There are two downsides. If a process dies before the lock file is deleted, you are stuck having to ftp in (or whatever) and delete it by hand. Also, you have to police your own locking and unlocking.

Link to comment
Share on other sites

Hey,

 

I follow your suggestion, i created a dummy.lock file to open and lock and then manage the other file, well it didn't work. I tried with two users acessing at the same time, and both did the code in the supposed locked part (just some echo's).

 

Ciao.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.