O'Reilly logo

Java and XML by Brett McLaughlin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Real World

We finish this chapter up as we have finished our others: with a look at issues that affect using the tools in this chapter in a real-world situation. In this chapter, these include threading issues with writing XML data, alternatives to using JDOM for writing XML data, and handling lost references to the XmlRpcConfiguration utility class.

Threading, Writing, and Arithmetic

Although we sped through our look at the saveConfiguration( ) method and how it handled writing out XML, you may have noticed something key in the method declaration:

/**
 * <p>
 * This will save the current state out to the specified 
 *   <code>OutputStream</code>.
 * </p>
 *
 * @throws <code>IOException</code> - when errors occur in saving.
 */
public synchronized void saveConfiguration(OutputStream out)
    throws IOException {

    // Method implementation
}

We use the synchronized keyword here to ensure that the lock for our XmlRpcConfiguration object is obtained before the configuration data is written. This is particularly important when using XML, as APIs like JDOM can be built on implementations that periodically reload the underlying data; in other words, changes by other programs to the XML data could cause serious errors or corrupted data if this method writes to the data as well.

Additionally, you should be very careful when multiple applications write to the same XML data source. Entire database systems have been written to handle pessimistic and optimistic locking, sharing data, and the other complex ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required