com.gigaspaces.hibernate
Class HibernateCacheLoaderImpl

java.lang.Object
  extended by com.gigaspaces.hibernate.HibernateCacheLoaderImpl
All Implemented Interfaces:
CacheIteratorFactory, CacheLifeCycleManager, CacheLoader
Direct Known Subclasses:
HibernateCacheStoreImpl

public class HibernateCacheLoaderImpl
extends Object
implements CacheLoader, CacheLifeCycleManager, CacheIteratorFactory

The HibernateCacheLoaderImpl used to load data from external database into GigaSpaces via Hibernate 3.0 ORM.
When a matching entry is not found in the space the HibernateCacheLoaderImpl is called to load the data from the database.
See the CacheLoader and CacheIteratorFactory for full description of the methods mapping between the Map/Space API and the HibernateCacheLoaderImpl methods.

Configuration
To use the HibernateCacheLoaderImpl the space schema XML configuration file should include the following. 
<space-config> <persistent> <enabled>true</enabled> <StorageAdapterClass>com.j_spaces.sadapter.cache.CacheAdapter</StorageAdapterClass> <CacheLoaderClass>com.gigaspaces.hibernate.HibernateCacheLoaderImpl</CacheLoaderClass> <StorageAdapterURL>/config/</StorageAdapterURL> </persistent> <engine> <cache_policy>0</cache_policy> </engine> </space-config>


You may also use the following space properties: space-config.persistent.enabled=true space-config.persistent.StorageAdapterClass=com.j_spaces.sadapter.cache.CacheAdapter space-config.persistent.CacheLoaderClass=com.gigaspaces.hibernate.HibernateCacheLoaderImpl space-config.engine.cache_policy=0 space-config.persistent.StorageAdapterURL= hibernate.cfg.xml location
When using the HibernateCacheLoaderImpl or HibernateCacheStoreImpl the space-config.persistent.StorageAdapterURL will be used to locate Hibernate hibernate.cfg.xml configuration mapping file. You must use space-config.engine.cache_policy=0 (LRU policy) to activate the CacheStore and CacheLoader implementation.
It is recommended to configure also the following to control the space memory usage and the eviction behavior:
space-config.engine.memory_usage.enabled=true
space-config.engine.memory_usage.high_watermark_percentage=95
space-config.engine.memory_usage.write_only_block_percentage=85
space-config.engine.memory_usage.write_only_check_percentage=76
space-config.engine.memory_usage.low_watermark_percentage=75
space-config.engine.memory_usage.eviction_batch_size=500

Note that only one, CacheLoader or CacheStore, but not both, is allowed to be used since CacheStore extends CacheLoader interface.

Since:
5.1

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.j_spaces.javax.cache.CacheLifeCycleManager
CacheLifeCycleManager.LifeCycleProperty
 
Constructor Summary
HibernateCacheLoaderImpl()
           
 
Method Summary
 void dropTable(String className)
          drop table represented by this class name.
 Properties init(String dbURL, String user, String password, Properties properties)
          Called when space is started
 CacheIterator iterator(CacheQuery exprationEntry)
          Returns an instance of Iterator which holds a matching candidates.
 Object load(Object key)
          Loads value from secondary data storage in case if it's not in cache.
 Map loadAll(Collection keys)
          Loads multiple values from secondary data storage in case if they are not in cache.
 void shutdown()
          Called when space is shut down
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateCacheLoaderImpl

public HibernateCacheLoaderImpl()
Method Detail

init

public Properties init(String dbURL,
                       String user,
                       String password,
                       Properties properties)
Called when space is started

Specified by:
init in interface CacheLifeCycleManager
Parameters:
dbURL - - The StorageAdapterURL space config/schema property value
user - - The userName space config/schema property value
password - - The password space config/schema property value
properties - - Properties taken from property files
Returns:
property indicators between the underlying driver and the cache adapter; can be null.

iterator

public CacheIterator iterator(CacheQuery exprationEntry)
Description copied from interface: CacheIteratorFactory
Returns an instance of Iterator which holds a matching candidates.

Specified by:
iterator in interface CacheIteratorFactory
Parameters:
exprationEntry -
Returns:
The generated Iterator

load

public Object load(Object key)
Loads value from secondary data storage in case if it's not in cache.

Specified by:
load in interface CacheLoader
Parameters:
key - Key of the object to load.
Returns:
Object returned by load; can be null
Space API Implementation Example:

        public Object load(Object key) {

                Object values[] = null;
                Object loaded_object = null;

                try {
                        Object myObject = getObject((IGSEntry) key);
                        int keyValue = 0;
                        if (myObject instanceof Person) {
                                Person person = null;
                                // This is the template we got - must have ID
                                person = (Person) myObject;
                                // getting primary key value - another option could be via
                                // ((IGSEntry) key).getPrimaryKeyName()
                                keyValue = person.getId().intValue();
                                // Constructing the query
                                Connection con = getConnection();

                                PreparedStatement stP = con.prepareStatement(
                                                "select * from  "
                                                                + tableNames.get(((IGSEntry) key)
                                                                                .getClassName()) + " where ID = ? ");
                                stP.setInt(1, keyValue);

                                ResultSet rs = stP.executeQuery();
                                int sz = rs.getMetaData().getColumnCount();

                                values = new Object[sz];
                                while (rs.next()) {
                                        for (int i = 0; i < sz; i++) {
                                                values[i] = rs.getObject(i + 1);

                                        }
                                        // here we construct the loaded Person object
                                        loaded_object = new Person(String.valueOf(values[0]),
                                                        String.valueOf(values[1]), Integer.valueOf(String
                                                                        .valueOf(values[2])));
                                        break;
                                }
                                rs.close();
                                rs.close();
                                con.close();
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                }
                return getConvertor().toIGSEntry(loaded_object);
        }


Map API Implementation Example:
          
        public Object load(Object key) {

                Object values[] = null;
                Person loaded_person = null;
                Connection con = null;
                try {
                        con = getConnection();
                        int keyValue = 0;
                        Map.Entry mEntry = ((IGSEntry) key).getMapEntry();

                        if (mEntry != null) {
                                keyValue = ((Integer) mEntry.getKey()).intValue();
                        } else {
                                keyValue = ((Integer) getId((IGSEntry) key)).intValue();
                        }

                        PreparedStatement stP = con.prepareStatement("select * from  " + tableNames.get(Person.class.getName())
                                                        + " where ID = ? ");
                        stP.setInt(1, keyValue);

                        ResultSet rs = stP.executeQuery();
                        int sz = rs.getMetaData().getColumnCount();

                        values = new Object[sz];
                        while (rs.next()) {
                                for (int i = 0; i < sz; i++) {
                                        values[i] = rs.getObject(i + 1);
                                }
                                // HERE WE MAP table row to Person object
                                loaded_person = new Person(String.valueOf(values[0]), String
                                                .valueOf(values[1]), Integer.valueOf(String
                                                .valueOf(values[2])));
                                break;
                        }
                        rs.close();
                        con.close();

                } catch (Exception e) {
                        try {
                                con.close();
                        } catch (SQLException e1) {
                                e1.printStackTrace();
                        }
                        e.printStackTrace();
                }
                return getConvertor().toIGSEntry(loaded_person);
        }

                

loadAll

public Map loadAll(Collection keys)
Loads multiple values from secondary data storage in case if they are not in cache.

Specified by:
loadAll in interface CacheLoader
Parameters:
keys - Collection of cache keys values for which need to be loaded.
Returns:
Map of key-value pairs loaded from data storage.

shutdown

public void shutdown()
Called when space is shut down

Specified by:
shutdown in interface CacheLifeCycleManager

dropTable

public void dropTable(String className)
Description copied from interface: CacheLifeCycleManager
drop table represented by this class name.

Specified by:
dropTable in interface CacheLifeCycleManager
Parameters:
className - representing the table to be dropped.