A common issue I’m facing recently is how to integrate existing tier-based applications with GigaSpaces persistency service, AKA persistency as a service (Paas) or mirror . The motivation is often a result of the acknowledgment that a standard tier based application fails to scale when facing the database throughput limitation.
Software Caching technologies (overlooking their complexity and maintenance they add to the stack) are useful for read operations. You’ll find out quite fast that operations as insert or update under a transaction require synchronous disk write operations, which are expensive both in performance and cost perspectives. Storage devices (NAS/DAS) together with database clustering software are becoming major investments for large-scale applications.
GigaSpaces XAP/EDG provides both caching and persistency services based on its memory virtualization capability. GigaSpaces XAP/EDG simply utilize the available machine’s memory on the cloud (grid computing). Using the space as a fast clustered in-memory entity, the transactions are replicated into a minimum of two separate physical machines. The persistency service is an asynchronous (thus out of the latency path) service that stores the data into the disk using the CacheStore interface.
Now comes our out of the box Hibernate CacheStore implementation, where once the mirror service is configured to use Hibernate, it will store the data asynchronously based on the hibernate mapping files (*.hbm.xml) that contain the object-relational mapping metadata.
The nice thing about using Hibernate and GigaSpaces is that the combination of the two provides a non-intrusive way to keep your programming model and services as POJOs (plain old java objects) that are abstracted from the caching or disk implementation. This is done by either using annotations or external XML descriptor files.
So after reviewing the theory, I thought you might be interested in a practical example, showing how to integrate GigaSpaces mirror service into an existing tier-based application; see below.
Example: Integrating GigaSpaces Mirror Service into an Existing Tier Based Application
The Hibernate mapping implementation is encapsulated in GigaSpaces mirror service, meaning this can be a non-intrusive procedure in existing projects. Using hibernate-tools you can auto-generate your Pojo sources and hbm files using existing database tables just by configuring the database connection.
There are 2 options of using hibernate tools:
- Using a simple ant + hibernate tools jar file.
- Using an up to date eclipse plug-in; more details.
This example uses option one, which requires the download of the workshop toolset; download.
Install & config:
- Unzip the workshop_tools tar.gz file into a working directory
- Make sure ant is installed (http://ant.apache.org/)
- Configure the build.xml to match your JDBC and package settings
Adding a usage target to the build.xml will give you a nice default option menu:
<echo message=”Integrating GigaSpaces build script”/>
<echo message=”Among the available targets are:”/>
<echo message=”clean –> Delete all generated demo files.”/>
<echo message=”bottomup.middlegen –> Run Middlegen and generate Hibernate mapping files.”/>
<echo message=”bottomup.hbm2java –> Generate .java from .hbm files.”/>
<echo message=”gui.hibernate –> Run hibern8ide for ad-hoc queries.”/>
Remember to point usage target as default target:
<project name=”workshop-toolset” default=”usage” >
Running ant should produce the following:
Doing some “reverse engineering” by creating hibernate mapping files (hbm.xml) files out of existing database tables
- Configure the target using your JDBC properties (set the package to your preference)
- Execute Ant bottomup.middlegen command
Update the build.xml jdbc configuration:
description=”Run Middlegen and generate Hibernate mapping files.”>
Running: ant bottomup.middlegen
Select the tables you wish to generate object from and click Generate
Note: since this is an old hibernate tools version, you’ll need to update the DOCTYPE header to:
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
Create the POJOs out of the created hibernate mapping files (see: Step one)
- Using Ant bottomup.hbm2java command
- Create GigaSpaces mapping files (gs.xml); see instructions
- Or use GigaSpaces annotations; see instructions
Enabling the mirror service; see details and instructions.
Using the hibernate gui to validate the hibernate.config.xml and query the DB:
If you’re interested in some further reading, here is a useful link for Hibernate ORM users.
I’d be happy to hear your thoughts, or try it out and let me know how it worked for you!