Summary: Using GigaSpaces as a Hibernate cache provider; creating a Hibernate-integrated Data Grid with your existing ORM.
OverviewThis page explains how you can use GigaSpaces eXtreme Application Platform (XAP) to:
GigaSpaces as Hibernate 2nd level cache provides:
Read further in this section for more details about these solutions, or jump to detailed instructions: how to get a free distributed second-level cache or how to add a GigaSpaces data grid.
The Existing ArchitectureYour existing Hibernate-based database integration probably looks like the following: the application uses Hibernate to persist its data directly to the database. Step 1: Getting a Free Distributed Two-Level CacheYou can very easily replace your existing Hibernate cache provider, typically EHCache, with GigaSpaces. This gives you a powerful distributed cache with multiple clients. Benefits:
Step 2: Scaling Up Your Database by Adding a Data GridIf you need to scale up your database, you can use GigaSpaces as an In-Memory Data Grid. Your application communicates with the Data Grid using The GigaSpace Interface or the Map API interfaces. On the back-end, GigaSpaces persists the data to your existing database using your existing Hibernate O/R mapping in asynchronous manner. Benefits:
Beyond the Data Grid: Scaling Out Your Application Using Space-Based ArchitectureTo gain ultimate scalability for your application, you can package your business logic together with the GigaSpaces space - which provides not only data but also messaging functionality. This creates a Processing Unit which is completely self sufficient (not dependent on a database, messaging server or any central component), meaning that you to duplicate it as many times as necessary without increasing complexity. This approach is called Space Based Architecture, and the way to achieve it is the Spring-based OpenSpaces platform, which allows you to deploy your application as a set of services encased in a Processing Unit.
How to Switch Your Cache Provider to GigaSpaces and Gain Distributed CachingThis section provides instructions on switching your existing Hibernate local cache, typically EhCache, with GigaSpaces. This will give you a powerful distributed cache with multiple clients and advanced clustering. 1. Download the GigaSpaces (
Spring Configuration
Spring Configuration • Set hibernate.cache.provider_class to org.openspaces.hibernate.cache.SimpleMapCacheProvider: <prop key="hibernate.cache.provider_class"> org.openspaces.hibernate.cache.SimpleMapCacheProvider</prop> • Set hibernate.cache.use_second_level_cache to true: <prop key="hibernate.cache.use_second_level_cache">true</prop> • Set gigaspace.hibernate.cache.url to /./dataGrid: <prop key="gigaspace.hibernate.cache.url">/./dataGrid</prop> Direct Configuration (hibernate.properties) Direct Configuration (hibernate.properties Edit your hibernate.properties file. Add or modify the following properties in the Second-Level Cache section: • Set hibernate.cache.provider_class to org.openspaces.hibernate.cache.SimpleMapCacheProvider: hibernate.cache.provider_class org.openspaces.hibernate.cache.SimpleMapCacheProvider • Set hibernate.cache.use_second_level_cache to true: hibernate.cache.use_second_level_cache true • Set gigaspace.hibernate.cache.url to /./dataGrid: gigaspace.hibernate.cache.url /./dataGrid Direct Configuration (hibernate.cfg.xml) Direct Configuration (hibernate.cfg.xml) • Set hibernate.cache.provider_class to org.openspaces.hibernate.cache.SimpleMapCacheProvider: <property name="hibernate.cache.provider_class"> org.openspaces.hibernate.cache.SimpleMapCacheProvider</property>
4. Set or add the cache usage (the cache concurrency strategy) in your mapping resource file (*.hbm.xml files) to read-only, read-write or nonstrict-read-write: <class name="your.class.name" table="YourTableName"> <cache usage="nonstrict-read-write"/> Alternatively, use the <class-cache> and <collection-cache> elements in your hibernate configuration file. You must include the following JARs from the Hibernate distribution package: jdbc2_0-stdext.jar; dom4j-XXX.jar; commons-collections-XXX.jar; asm.jar; asm-attrs.jar; antlr-XXX.jar; ejb3-persistence.jar; hibernate3.jar; hibernate-annotations.jar; hibernate-commons-annotations.jar 5. Since your Hibernate objects and their custom type will be eventually be serialized across to wire stored on remote space or replicated into a backup space In-Memoery, these need to be serialized. You should make all your Hibernate objects and their custom fields will be implementing Serializable.
Remote Cache
Remote Cache Topology To use the remote cache topology:
Master-Local Cache Master-Local Cache Topology To use the master-local topology:
Wait to see the following output: Found 1 GSMs Deploying [datagrid] with name [datagrid] under groups [gigaspaces-7.1.2-XAPPremium-ga] and locators [] SLA Not Found in PU. Using Default SLA. Waiting for [1] processing unit instances to be deployed... [datagrid] [1] deployed successfully on [10.10.254.1] Finished deploying [1] processing unit instances
Partitioned Master-Local Cache Partitioned Master-Local Cache Topology To use the master-local partitioned topology with two partitions:
Wait to see the following output: gs> deploy-space -cluster schema=partitioned total_members=2,0 datagrid Found 1 GSMs Deploying [datagrid] with name [datagrid] under groups [gigaspaces-7.1.2-XAPPremium-ga] and locators [] SLA Not Found in PU. Using Default SLA. Overrding SLA cluster schema with [partitioned] Overrding SLA numberOfInstances with [2] Overrding SLA numberOfBackups with [0] Waiting for [2] processing unit instances to be deployed... [datagrid] [1] deployed successfully on [10.10.254.1] [datagrid] [2] deployed successfully on [10.10.254.1] Finished deploying [2] processing unit instances
Embedded Replicated Cache Embedded Replicated Cache Topology To use the embedded replicated topology with two replicated instances:
Basic Guidelines
Settings GigaSpaces local cache with Hibernate Second Level CacheWhen using Hibernate Second Level Cache you can configure also GigaSpaces local cache. See below an example for both: <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.${jdbc.platform}Dialect</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.show_sql">{hibernate.show_sql}</prop> <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.transaction.flush_before_completion">true</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.cache.user_structured_entries">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.transaction.auto_close_session">true</prop> <prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="gigaspace.hibernate.cache.url">jini://*/*/dataGrid?useLocalCache</prop> <prop key="space-config.engine.cache_size">5000000</prop> <prop key="space-config.engine.memory_usage.high_watermark_percentage">65</prop> <prop key="space-config.engine.memory_usage.write_only_block_percentage">63</prop> <prop key="space-config.engine.memory_usage.write_only_check_percentage">60</prop> <prop key="space-config.engine.memory_usage.low_watermark_percentage">55</prop> <prop key="space-config.engine.memory_usage.eviction_batch_size">1000</prop> <prop key="space-config.engine.memory_usage.retry_yield_time">100</prop> <prop key="space-config.engine.memory_usage.retry_count">20</prop> </props> </property> Custom Hibernate Second Level Cache ImplementationYou may implement your own custom Hibernate Second Level Cache by extending the AbstractMapCacheProvider and implementing the buildCache method. The implemented class name should be set as the hibernate.cache.provider_class value. See below example how you may create a custom Hibernate Second Level Cache implementation that will have a different TTL for each cache region: public class CustomMapCacheProvider extends AbstractMapCacheProvider { public Cache buildCache(String regionName, Properties properties) throws CacheException { Long ttl = 0l; if (regionName.equals(Person.class.getName())) { ttl = 60000l; //TODO: make this configurable } else if (regionName.equals(Car.class.getName())) { ttl = 120000l; } else if (regionName.equals(House.class.getName())) { ttl = 180000l; } else { ttl = getTimeToLive(); } return new SimpleMapCache(regionName, getMap(), ttl, getWaitForResponse()); } } GigaSpaces Hibernate 2nd Level Cache PropertiesGigaSpaces Hibernate 2nd Level Cache controlled via the following system properties:
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |