|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.gigaspaces.hibernate.HibernateCacheLoaderImpl
public class HibernateCacheLoaderImpl
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.When using the<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
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
orCacheStore
, but not both, is allowed to be used sinceCacheStore
extendsCacheLoader
interface.
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 |
---|
public HibernateCacheLoaderImpl()
Method Detail |
---|
public Properties init(String dbURL, String user, String password, Properties properties)
init
in interface CacheLifeCycleManager
dbURL
- - The StorageAdapterURL space config/schema property valueuser
- - The userName space config/schema property valuepassword
- - The password space config/schema property valueproperties
- - Properties taken from property files
public CacheIterator iterator(CacheQuery exprationEntry)
CacheIteratorFactory
iterator
in interface CacheIteratorFactory
exprationEntry
-
public Object load(Object key)
load
in interface CacheLoader
key
- Key of the object to load.
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); }
public Map loadAll(Collection keys)
loadAll
in interface CacheLoader
keys
- Collection of cache keys values for which need to be loaded.
public void shutdown()
shutdown
in interface CacheLifeCycleManager
public void dropTable(String className)
CacheLifeCycleManager
dropTable
in interface CacheLifeCycleManager
className
- representing the table to be dropped.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |