|
GigaSpaces XAP 7.0 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.gigaspaces.datasource.hibernate.HibernateMapDataSource
@Deprecated public class HibernateMapDataSource
HibernateMapDataSource is a full Hibernate implementation of IMap external data source. Changing data - when map data is put/removed HibernateMapDataSource is responsible to perform the relevant changes at the external database. HibernateMapDataSource delegates these changes into external database via Hibernate 3.0 ORM. Reading data - when the space is queried about specific key and it is not found in the space HibernateMapDataSource is called to load the data from the database.CacheLoader
andCacheStore
are used for data loading/storing. Bulk operations - in case of transactions and replication will invoke theBulkDataPersister
methods for persisting data. See theMapDataSource
for full description of the methods mapping between the Map API and theHibernateMapDataSource
methods.
Configuration To use theHibernateMapDataSource
the space schema XML configuration file should include the following. <space-config> <persistent> <enabled>true</enabled> <StorageAdapterClass>com.j_spaces.sadapter.datasource.DataAdapter</StorageAdapterClass> </persistent> <external-data-source> <data-source-class>com.gigaspaces.datasource.hibernate.HibernateMapDataSource</data-source-class> <data-class>java.lang.Object</data-class> <supports-inheritance>true</supports-inheritance> <supports-version>false</supports-version> <usage>read-write</usage> </external-data-source> <engine> <cache_policy>0</cache_policy> </engine> </space-config> You may also use the following space properties: space-config.persistent.enabled=true<.b> space-config.persistent.StorageAdapterClass=com.j_spaces.sadapter.datasource.DataAdapter space-config.engine.cache_policy=0 space-config.external-data-source.data-source-class=com.gigaspaces.datasource.hibernate.HibernateMapDataSource space-config.external-data-source.init-properties-file=hibernate.properties When using theHibernateMapDataSource
the space-config.external-data-source.init-properties the content of this file will be passed on init to theHibernateMapDataSource
. You must use space-config.engine.cache_policy=0 (LRU policy) to activate the external datasource 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
Field Summary | |
---|---|
protected Hashtable<String,org.hibernate.metadata.ClassMetadata> |
_metaDataTable
Deprecated. |
protected org.hibernate.SessionFactory |
_sessionFactory
Deprecated. |
Fields inherited from interface com.gigaspaces.datasource.ManagedDataSource |
---|
DATA_CLASS_PROPS, NUMBER_OF_PARTITIONS, STATIC_PARTITION_NUMBER |
Constructor Summary | |
---|---|
HibernateMapDataSource()
Deprecated. |
Method Summary | |
---|---|
DataIterator<Map.Entry<Object,Object>> |
entries()
Deprecated.
Creates and returns an iterator over all the entries in data source. |
void |
erase(Object key)
Deprecated. Remove the specified key from the underlying store if present. |
void |
eraseAll(Collection keys)
Deprecated. Remove the specified keys from the underlying store if present |
void |
executeBulk(List<BulkItem> bulk)
Deprecated. This method is called in the following scenarios: 1. |
protected Object |
getId(Object template)
Deprecated. Return pojo identifier |
protected String |
getMapObjectsClass()
Deprecated. Return map objects class name |
protected org.hibernate.metadata.ClassMetadata |
getMetadata(Object entry)
Deprecated. Return pojo entry metadata |
void |
init(Properties properties)
Deprecated. Initialize and configure the data source using given properties. |
DataIterator<Map.Entry<Object,Object>> |
initialLoad()
Deprecated.
Creates and returns an iterator over all the entries that should be loaded into space. |
Object |
load(Object key)
Deprecated. Loads value from secondary data storage in case if it's not in cache. |
Map |
loadAll(Collection keys)
Deprecated. Loads multiple values from secondary data storage in case they are not in cache. |
void |
shutdown()
Deprecated. Close the data source and clean any used resources. |
void |
store(Object key,
Object value)
Deprecated. Store the specified values under the specified keys in the store. |
void |
storeAll(Map map)
Deprecated. Store the specified values under the specified keys in the underlying store. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected org.hibernate.SessionFactory _sessionFactory
protected Hashtable<String,org.hibernate.metadata.ClassMetadata> _metaDataTable
Constructor Detail |
---|
public HibernateMapDataSource()
Method Detail |
---|
public void init(Properties properties) throws DataSourceException
ManagedDataSource
init
in interface ManagedDataSource<Map.Entry<Object,Object>>
properties
- - contains user defined param and Partition data
DataSourceException
public void shutdown() throws DataSourceException
ManagedDataSource
shutdown
in interface ManagedDataSource<Map.Entry<Object,Object>>
DataSourceException
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)
CacheLoader
loadAll
in interface CacheLoader
keys
- Collection of (CacheQuery) Keys to load by.
Space API Implementation Examplepublic Map loadAll(Collection keys) { HashMap map = new HashMap(); Iterator keyIter = keys.iterator(); while (keyIter.hasNext()) { CacheQuery exprationEntry = (CacheQuery) keyIter.next(); Object query = exprationEntry.getQuery(); String querystr = null; // null template if (query instanceof IGSEntry) { IGSEntry igsentry = (IGSEntry) query; if (!tableNames.containsKey(igsentry.getClassName())) { System.out.println("Do not have mapping for class " + igsentry.getClassName()); if (igsentry.getClassName().equals("java.lang.Object")) { System.out.println("HERE YOU CAN LOAD DATA INTO THE SPACE WHEN IT IS STARTED!"); } // return empty map - indicates no objects loaded from database return map; } querystr = "select * from " + tableNames.get(igsentry.getClassName()); } // non null template or SQLQuery used if (query instanceof SQLQuery) { SQLQuery sqlquery = (SQLQuery) query; if (!tableNames.containsKey(sqlquery.getClassName())) { System.out.println("Do not have mapping for class " + sqlquery.getClassName()); // return empty map - indicates no objects loaded from database return map; } querystr = "select * from " + tableNames.get(sqlquery.getClassName()); querystr += " where " + sqlquery.getQuery(); } try { Connection con = getConnection(); PreparedStatement stP = con.prepareStatement( querystr); ResultSet rs = stP.executeQuery(); IGSEntry value = null; while (rs.next()) { Integer id = new Integer(rs.getInt(3)); Person person = new Person(rs.getString(1), rs.getString(2), id); value = getConvertor().toIGSEntry(person); map.put(value, value); } rs.close(); con.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } return map; }
Map API Implementation Examplepublic Map loadAll(Collection keys) { HashMap map = new HashMap(); Iterator keyIter = keys.iterator(); while (keyIter.hasNext()) { CacheQuery cacheQuery = (CacheQuery) keyIter.next(); Object query = cacheQuery.getQuery(); String sqlstr = null; sqlstr = "select * from Person"; Map.Entry mapentry = null; if (query instanceof Map.Entry) { mapentry = (Map.Entry)query; sqlstr += " where id = " + Integer.valueOf(mapentry.getKey().toString()).intValue(); } else if (query instanceof IGSEntry) { // All entries - for example IMap.values() call IGSEntry igsentry = (IGSEntry)query; if (igsentry.getClassName().equals("java.lang.Object")) { return map; } } else if (query instanceof SQLQuery) { SQLQuery sqlquery = (SQLQuery)query; if (sqlquery.getClassName().equals("java.lang.Object")) { return map; } sqlstr += " where " + sqlquery.getQuery(); } PreparedStatement stP =null; ResultSet rs = null; Connection con = null; try { con = getConnection(); stP = con.prepareStatement(sqlstr); rs = stP.executeQuery(); while (rs.next()) { Integer id = new Integer(rs.getInt(3)); Person person = new Person(rs.getString(1), rs.getString(2), id); IGSEntry igsEntry = getConvertor().toIGSEntry(person); map.put( igsEntry , igsEntry ); } rs.close(); con.close(); } catch (Exception e) { try { rs.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { con.close(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); throw new RuntimeException(e); } } return map; }
public void store(Object key, Object value)
store
in interface CacheStore
key
- key to store the value undervalue
- value to be storedpublic void storeAll(Map map)
storeAll
in interface CacheStore
map
- a Map of any number of keys and values to storepublic void erase(Object key)
erase
in interface CacheStore
key
- key whose mapping is being removed from the cachepublic void eraseAll(Collection keys)
CacheStore
eraseAll
in interface CacheStore
keys
- keys whose mappings are being removed from the cachepublic void executeBulk(List<BulkItem> bulk) throws DataSourceException
1. Transaction commit
2. Asynchronize persistency mode This method can throw DataSourceException.
This exception will be delivered to the client application wrapped by
com.j_spaces.javax.cache.CacheException
executeBulk
in interface BulkDataPersister
bulk
- This bulkSet contains set of BulkItem
DataSourceException
protected Object getId(Object template)
template
-
protected org.hibernate.metadata.ClassMetadata getMetadata(Object entry)
entry
-
protected String getMapObjectsClass()
public DataIterator<Map.Entry<Object,Object>> initialLoad() throws DataSourceException
ManagedDataSource
Creates and returns an iterator over all the entries that should be loaded into space.
initialLoad
in interface ManagedDataSource<Map.Entry<Object,Object>>
DataIterator
or null if no data should be loaded into space
DataSourceException
public DataIterator<Map.Entry<Object,Object>> entries() throws DataSourceException
BasicCacheLoader
Creates and returns an iterator over all the entries in data source.
Invoked on values() method
entries
in interface BasicCacheLoader<Object,Object>
DataIterator
or null if no data should be loaded into space
DataSourceException
|
GigaSpaces XAP 7.0 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |