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

Overview

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.

Benefits:

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

Benefits:

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

    Spring Configuration

    Spring Configuration

    Edit your SessionFactory Setup. This is the section inside your Spring Application Context File that controls the relevant Hibernate configuration (refer to the Spring documentation, ORM Data Access , section 12.2.2).
    Add or modify the following properties under <property name="hibernateProperties">:

    •  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 /./space:

    <prop key="gigaspace.hibernate.cache.url">/./space</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 /./space:

    gigaspace.hibernate.cache.url /./space
    

    Direct Configuration (hibernate.cfg.xml)

    Direct Configuration (hibernate.cfg.xml)

    Edit your hibernate.cfg.xml file. Add or modify the following properties under <session-factory>:

    •  Set hibernate.cache.provider_class to org.openspaces.hibernate.cache.SimpleMapCacheProvider:

    <property name="hibernate.cache.provider_class">
    	org.openspaces.hibernate.cache.SimpleMapCacheProvider</property>

    •  Set hibernate.cache.use_second_level_cache to true:

    <property name="hibernate.cache.use_second_level_cache">true</property>

    •  Set gigaspace.hibernate.cache.url to /./space:

    <property name="gigaspace.hibernate.cache.url">/./space</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-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; 
hibernate-commons-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.

    Master-Local Cache

    Master-Local Cache Topology

    In this topology each application has a lightweight, embedded cache, which is initially empty. The first time data is read, it is loaded from a master cache to the local cache (lazy load); the next time the same data is read, it is loaded quickly from the local cache. Later on data is either updated from the master or evicted from the cache.





    To use the master-local topology:

      1. Set your gigaspaces.hibernate.cache.url property to
        jini://*/*/dataGrid?useLocalCache&amp;groups=gigaspaces-6.0XAP
        
      2. Copy <GigaSpaces Root>\lib\hibernate\hibernate3.jar and <GigaSpaces Root>\lib\common\commons-logging.jar into the <GigaSpaces Root>\lib\ext folder.
      3. Copy dom4j.jar from Hibernate distribution package into the <GigaSpaces Root>\lib\ext foler.
      4. Run <GigaSpaces Root>\bin\gs-all.bat (.sh) to start a GigaSpaces container manager (GSM) and a container (GSC) for the master GigaSpaces space.
        Wait to see the following output in both GSM and GSC console windows:
        INFO [org.jini.rio.cybernode]: Registered to a ProvisionManager
        
      5. Run <GigaSpaces root>\bin\gs.bat pudeploy ..\deploy\templates\datagrid to deploy the master space
        Wait to see the following output (below the output shown in the previous step) in both GSM and GSC console windows:
        FINE [org.jini.rio.monitor.services]: [..\deploy\templates\datagrid:
        	..\deploy\templates\datagrid.PU] service provisioned, .....
        
      6. Run your application.

    Partitioned Master-Local Cache

    Partitioned Master-Local Cache Topology

    In this topology, data is split between two spaces (partitions) according to an index field defined in the data. An algorithm, defined in the load-balancing policy, maps values of the index field to specific partitions.

    This topology requires GigaSpaces eXtreme Application Platform ( Product Info Download Page) or GigaSpaces XAP Enterprise Data Grid ( Product Info Download Page) for multiple server nodes.



    To use the master-local partitioned topology with two partitions:

        1. Configure your gigaspaces.hibernate.cache.url property to:
          jini://*/*/dataGrid?useLocalCache&amp;groups=gigaspaces-6.0XAP
          
        2. Copy <GigaSpaces Root>\lib\hibernate\hibernate3.jar and <GigaSpaces Root>\lib\common\commons-logging.jar into the <GigaSpaces Root>\lib\ext folder.
        3. Copy dom4j.jar from Hibernate distribution package into the <GigaSpaces Root>\lib\ext folder.
        4. Run <GigaSpaces Root>\bin\gsm.bat (.sh) to start a container manager (GSM).
        5. Run <GigaSpaces Root>\bin\gsc.bat (.sh) to start a container (GSC) for the master space.
        6. Run <GigaSpaces Root>\bin\gsc.bat (.sh) to start an additional container (GSC).

          Wait to see the following output in the two GSC output windows:

          INFO [org.jini.rio.cybernode]: Registered to a ProvisionManager
          
        7. Run <GigaSpaces Root>\bin\gs pudeploy -cluster schema=partitioned total_members=2,0 ../deploy/templates/datagrid to run a cluster of two partitioned master spaces.

          Wait to see the following output in the first GSC console window:

          FINE [org.jini.rio.cybernode]: /datagrid.PU [1]: advertised
          

          and the following output in the second GSC console window:

          FINE [org.jini.rio.cybernode]: /datagrid.PU [2]: advertised
          
        8. Run your application.

    Embedded Replicated Cache

    Embedded Replicated Cache Topology

    This topology consists of two or more space instances with replication between them, allowing two or more applications to work with their own dedicated data store, while working on the same data as the other applications.

    This topology requires GigaSpaces eXtreme Application Platform ( Product Info Download Page) or GigaSpaces XAP Enterprise Data Grid ( Product Info Download Page) for multiple server nodes.



    To use the embedded replicated topology with two replicated instances:

          1. In your first application, configure the gigaspaces.hibernate.cache.url property to the following (as one long line):
            /./space?cluster_schema=sync_replicated&amp;total_members=2&amp;
            	id=1&amp;groups=gigaspaces-6.0XAP
            
          2. Run your first application.
          3. In your second application, configure your gigaspaces.hibernate.cache.url property to the following (as one long line):
            /./space?cluster_schema=sync_replicated&amp;total_members=2&amp;
            	id=2&amp;groups=gigaspaces-6.0XAP
            
          4. Run your second application.

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"/>
    </bean>
    
    <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 JavaSpaces or Map 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.
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence