XML Generation versus XML Database
Currently I'm using Hibernate to persist the entities within my app and I serve the entities back to my users in the form of XML. However this means that each time a resource is request I'm currently generating XML via Java which is ineffecent for a lot of reasons. I was wondering if I want to perhaps 开发者_如何学Gocreate a cache of Document objects when the application is running(i.e. batch load all the objects as Document objects as well) or perhaps use an XML based database in addition to MySQL/Hibernate...
You can use any caching solution -- like Ehcache or OSCache:
- when a client requests an xml, search the cache (by the id of the entity, presumably)
- if the document is not found in the cache, fetch it from DB and transform it to XML, then store it in the cache and return it
- whenever an entity is modified, invalidate its cache entry
But before you do this do an actual benchmark on how much CPU is consumed on generation and how often does it happen, and compare it to the memory required for having the cache.
Just in case, note that Hibernate allows to work with XML data. From the Chapter 18. XML Mapping:
Hibernate allows you to work with persistent XML data in much the same way you work with persistent POJOs. A parsed XML tree can be thought of as another way of representing the relational data at the object level, instead of POJOs.
Hibernate supports dom4j as API for manipulating XML trees. You can write queries that retrieve dom4j trees from the database and have any modification you make to the tree automatically synchronized to the database. You can even take an XML document, parse it using dom4j, and write it to the database with any of Hibernate's basic operations: persist(), saveOrUpdate(), merge(), delete(), replicate() (merging is not yet supported).
This feature has many applications including data import/export, externalization of entity data via JMS or SOAP and XSLT-based reporting.
A single mapping can be used to simultaneously map properties of a class and nodes of an XML document to the database, or, if there is no class to map, it can be used to map just the XML.
Not sure this will suit your needs (and I never used this not so well known feature). But maybe have a look.
精彩评论