Neil Burnett <[EMAIL PROTECTED]> wrote:

> I am updating an xml file with data from a web form. I simply add elements
> onto the end.
>
> What do I worry about where concurrency is concerned?
>
> 1       Do I read the xml file once when I initialize my servlet using JDOM,
> then synchronize each time I write it back to disk?
> 2       Or, should I read and write for every update?
>
> By the way, thanks to the authors for JDOM ( http://www.jdom.org ). Its just
> what I needed.

Nope, you have to read and write everytime...
The problem is how to synchronize it. I would do something like:

public class MyServlet ... {

  // This is a static variable, so there should be one instance of the
  // object per classloader.
  private static Object synch = new Object();

  public void do???(HttpServletRequest, HttpServletResponse) {
   ...
   synchronized (synch) {
     // Now we read the XML data
     ...
     // Now we modify the JDOM tree
     ...
     // Now we write it
  }
}

The synch obect, being a static variable is instantiated once per
classloader (so be sure you don't have the same servlet in two webapps,
because it will NOT be synchronized at that point), and will be used to
sinchronize your access to the file...

    Pier

--
Pier Fumagalli - Sun Microsystems Inc. - <mailto:[EMAIL PROTECTED]>
----------------------------------------------------------------------------

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to