XAP7 == "XAP9NET" || XAP7 == "XAP9" || XAP7 == "XAP9NET")

GigaSpaces for Hibernate ORM Users

Search XAP 7.0
Searching XAP 7.0.X Documentation
Browse XAP 7.0
Offline Documentation

Download latest offline documentation in HTML format:
xap-7.0.2-documentation.zip (12.3MB)


Summary: Using GigaSpaces as a Hibernate cache provider; creating a Hibernate-integrated Data Grid with your existing ORM.


This page explains how you can use GigaSpaces eXtreme Application Platform (XAP) to:

  • Get a free 2nd-level distributed cache for your Hibernate-based database integration
  • Scale up your database using the existing ORM by adding a GigaSpaces Data Grid with back-end Hibernate integration

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.

GigaSpaces fully supports the Spring Framework, and can be plugged in very easily without changing your existing code.

The Existing Architecture

Your 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 Cache

You can very easily replace your existing Hibernate cache provider, typically EHCache, with GigaSpaces. This gives you a powerful distributed cache with multiple clients.


  • Powerful distributed cache - multiple clients, each client's updates can be accessed by all other clients
  • Effortless integration using Spring

To learn how to do this, see How to Switch Your Second-Level Cache Provider to GigaSpaces below.

Step 2: Scaling Up Your Database by Adding a Data Grid

If 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 Spring-based OpenSpaces framework (or alternatively, using the simple JavaSpaces API or Map API interfaces). On the back-end, GigaSpaces persists the data to your existing database using your existing Hibernate O/R mapping.


  • Database scalability through partitioning and data distribution - enables higher data volumes and higher throughput with low latency
  • Better decoupling between your application and the database - no need to hard-wire Hibernate and database concepts into your code and runtime environment
  • Event-driven model enables notifications when data is modified
  • Database access can be synchronous or asynchronous - the GigaSpaces Mirror Service allows data to be persisted to the database asynchronously, without a performance penalty

To learn how to do this, see How to Add the GigaSpaces Data Grid.

Beyond the Data Grid: Scaling Out Your Application Using Space-Based Architecture

To 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.

To get started with SBA and Open Spaces see our eXtreme Application Platform Tutorial.

To learn more about Space-Based Architecture and its implications, read our white paper, The Scalability Revolution: From Dead End to Open Road.

To learn more about OpenSpaces, see the OpenSpaces section in this online help.

How to Switch Your Cache Provider to GigaSpaces and Gain Distributed Caching

This 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.

To switch your Hibernate cache provider to GigaSpaces and use GigaSpaces distributed caching:

  1. Download the GigaSpaces ( Product FAQ Download Page)
  2. Install GigaSpaces. If you need help, refer to the installation instructions.
  3. This step is different if you are managing your Hibernate configuration using Spring (refer to the Spring documentation, ORM Data Access*** , section 12.2.2), or directly using hibernate.properties or hibernate.cfg.xml. Select the relevant tab below.

  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-1.6.1.jar; commons-collections-2.1.1.jar; asm.jar; asm-attrs.jar;
antlr-2.7.6rc1.jar; ejb3-persistence.jar; hibernate3.jar; hibernate-annotations.jar; 
  1. Launch your application. The application should instantiate a local cache based on an embedded GigaSpaces space.

  2. If you want to use GigaSpaces as a distributed cache, select the distributed topology that interests you from the tabs below.

How to Add the GigaSpaces Data Grid

This section provides instructions on adding the GigaSpaces In-Memory Data Grid to your Hibernate database integration. This will allow you to scale up your central database by distributing and partitioning the data. Your application will communicate directly with GigaSpaces, and GigaSpaces will persist the data in the background to your existing database, using your existing O/R mapping. Persistency and data retrieval can be done synchronously, using the GigaSpaces Read/Write-Through mechanism, or asynchronously, using the GigaSpaces Mirror Service.

To add the GigaSpaces In-Memory Data Grid:

These instructions assume you are managing your Hibernate configuration using Spring. Refer to the Spring documentation, ORM Data Access to learn how to do this.

  1. If you haven't done so already, download the GigaSpaces eXtreme Application Platform ( Product Info Download Page) or GigaSpaces XAP Enterprise Data Grid ( Product Info Download Page). Note that you cannot use the Community Edition for this purpose, because a Data Grid requires more than one GigaSpaces server node.
  2. Install GigaSpaces. If you need help, refer to the installation instructions.
  3. Edit your SessionFactory Setup. This is the section in your Spring Application Context File that controls the relevant Hibernate configuration (refer to the Spring documentation, ORM Data Access , section 12.2.2).
  4. Add or modify the following properties under <property name="hibernateProperties">:
    • Set hibernate.cache.provider_class to org.hibernate.cache.NoCacheProvider (no cache provider):
      <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
    • Set hibernate.cache.use_second_level_cache to false:
      <prop key="hibernate.cache.use_second_level_cache">false</prop>
  5. Add the following bean definition to your application context:
    <bean id="hibernateDataSource" class="org.openspaces.persistency.hibernate.DefaultHibernateExternalDataSource">
        <property name="sessionFactory" ref="sessionFactory"/>
    <os-core:space id="space" url="/./space" schema="persistent" 
    external-data-source="hibernateDataSource" />
  6. Modify your application to handle data using GigaSpaces' Spring-based Open Spaces framework, instead of the Hibernate API. Keep in mind that GigaSpaces will continue to persist data to your existing database using your existing O/R mapping.

    To learn how to do this, refer to the Open Spaces section.

    If you prefer to access GigaSpaces directly using one of the supported APIs, refer to the The GigaSpace Interface and the POJO Support sections.

  7. Deploy the GigaSpaces Data Grid in the topology of your choice. A partitioned topology is particularly useful for scaling up to increase the throughput of your database.
  8. Choose how GigaSpaces should access the database:
    • If you want data to be synchronously read and written to the database by the Data Grid, refer to the External Data Source section.
    • If you want data to be asynchronously mirrored between the Data Grid and and the database, refer to the Mirror Service section.
  9. Launch your application. The application writes and reads data to/from the GigaSpaces In-Memory Data Grid; the data is persisted in the background to your existing database using Hibernate.

Settings GigaSpaces local cache with Hibernate Second Level Cache

When using Hibernate Second Level Cache you can configure also GigaSpaces local cache. See below an example for both:

<property name="hibernateProperties"> 
	<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>
IMPORTANT: This is an old version of GigaSpaces XAP. Click here for the latest version.