Java uses XML as a persistent storage medium implementation method

Source: Internet
Author: User
Tags html form thread

For some small programs that need to persist data, the traditional relational database is large and impractical, OO database has a learning curve problem, and the use of XML is a better choice. This article will discuss the design of a reasonable XML persistence solution.

Using XML as a persistence layer solution, its basic functionality is:

1. The CRUD function of the object (the base object in this case is the member Class).

2. Ensure thread safety and apply to both desktop and Web applications.

3. There is a cache, the storage medium that is, the XML file suddenly lost in the case can also work effectively.

This example uses the Memberservice and memberpersistence two classes to collaborate on these functions.

Memberservice is a bridge between business and persistence layers, for CRUD operations on member objects, and a built-in Hashtable to cache member objects, even if the files are suddenly lost. The data in the cache will not be affected. It has a built-in memberpersistence member to complete interaction with persistent media.

Implementation Add, delete, update three functions Add (), delete (), update () are synchronized with lockobj, so there is no need to worry about thread safety issues. Other functions do not modify members, so there is no requirement for synchronization.

try{
memberPersistence.add(member);
members.put(member.getId(), member);
return true;
}
catch(XmlFileWriteException ex){
  System.out.println("Member:" + member.getId() + " add error!");
  return false;
}

When Memberpersistence adds an object, the object is added to the members, which ensures that the cache is synchronized with the actual data, and if the order is reversed, the Memberpersistence object is not successful when it is added. There is a xmlfilewriteexception exception, which is also necessary to remove the object just added in the catch, which increases the complexity of the program, not as simple and efficient as the above approach.

About the procedure of the query function do not understand see http://www.blogjava.net/sitinspring/archive/2007/06/05/122119.html form three.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.