🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #1592

Re: File locking

#1592
From: Tad Frysinger [TeamB]
Date: Tue, 21 Oct 2003 15:29
57 lines
3348 bytes
Craig Wilson wrote:

> I'm using xml files as a sort of database, with Castor doing the reading and
> writing.  I want to make the program multiuser, so that when one user
> accesses a certain piece of data, no one else can edit it, until the first
> guy is done with it.  The method I'd like to try is to have a text
> (preferably xml) file that would contain a list of locked data elements.
> When you are ready to work on some data, you would have to open that file
> and check to make sure that data element isn't listed.  If it's there, show
> a please try again later message, otherwise write that data element to the
> file and proceed, then remove that element from the file when finished with
> it.  But for that to be safe, only one person can be allowed to have the
> file open at any one time.  Is there a way to lock the file for this
> purpose?  Or any other suggestions would be appreciated.
> Craig
> 
> 
Well, concurrent access is the classic reason to use a database, because 
it normally handles the associated lock release problems when a client 
that originally locked the resource "goes away". If you don't want to do 
that, one common approach is to use another file as a semaphore. The 
semaphore is a "logical lock" that signals other clients not to access 
the resource it is associated with.

So, for example, when you want to access the XML file, you do something 
like this:

try {
    File f = new File( MY_SEMAPHORE_FILE );
    boolean createdSemaphore = false;
    while ( !createdSemaphore &&
            <other conditions like a timeout for example> ) {
       try {
          createdSemaphore = f.createNewFile();
          if ( createdSemaphore ) {
             // I created the semaphore, so I can access the XML file
             <do XML access here>

          }
       } finally {
          if ( createdSemaphore ) {
             try { f.delete() } catch (Exception e ) {};
          }
       }
    }
} catch (Exception e ) {
   // Handle it
}


The downside to this approach is that if the client creates the 
semaphore, then crashes without deleting the file, your other clients 
are stuck. Again, this is one of the advantages of a real database, that 
will release locks held by a client that it deems "has gone away".

-- 
Tad Frysinger [TeamB]
Technical Details
Message-ID: <3f9550ed$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f8d8436@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,b88831aa14954b88
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2003-10-21 08:46:13 PST
Date: Tue, 21 Oct 2003 09:29:33 -0600
From: "Tad Frysinger [TeamB]" <tfrysinger@earthlink.net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: File locking
References: <3f8d8436@newsgroups.borland.com>
In-Reply-To: <3f8d8436@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 206.61.185.113
Message-ID: <3f9550ed$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1066750189 206.61.185.113 (21 Oct 2003 08:29:49 -0700)
Lines: 58
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail