com.gigaspaces.datasource.hibernate
Class HibernateMapDataSource

java.lang.Object
  extended by com.gigaspaces.datasource.hibernate.HibernateMapDataSource
All Implemented Interfaces:
BulkDataPersister, ManagedDataSource<Map.Entry<Object,Object>>, MapDataSource<Object,Object>, BasicCacheLoader<Object,Object>, CacheLoader, CacheStore

Deprecated. use org.openspaces.persistency.hibernate package instead

@Deprecated
public class HibernateMapDataSource
extends Object
implements MapDataSource<Object,Object>

 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 and  CacheStore are used for data loading/storing.
 
 Bulk operations - in case of transactions and replication will invoke the BulkDataPersister methods for persisting data.
 
 See the  MapDataSource for full description of the methods mapping between the Map API and the HibernateMapDataSource methods.
Configuration To use the HibernateMapDataSource 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 the HibernateMapDataSource the space-config.external-data-source.init-properties the content of this file will be passed on init to the HibernateMapDataSource. 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

Since:
6.0

Field Summary
 
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.
 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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateMapDataSource

public HibernateMapDataSource()
Deprecated. 
Method Detail

init

public void init(Properties properties)
          throws DataSourceException
Deprecated. 
Description copied from interface: ManagedDataSource
Initialize and configure the data source using given properties.
Called when space is started.
The properties are loaded from a file that can be defined in the space schema or as a property named:

space-config.ExternalDataSource.properties-file partitionId and number of partitions are also in the Properties - can be read with STATIC_PARTITION_NUMBER and NUMBER_OF_PARTITIONS

Specified by:
init in interface ManagedDataSource<Map.Entry<Object,Object>>
Parameters:
properties - - contains user defined param and Partition data
Throws:
DataSourceException

shutdown

public void shutdown()
              throws DataSourceException
Deprecated. 
Description copied from interface: ManagedDataSource
Close the data source and clean any used resources.
Called before space shutdown.

Specified by:
shutdown in interface ManagedDataSource<Map.Entry<Object,Object>>
Throws:
DataSourceException

load

public Object load(Object key)
Deprecated. 
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)
Deprecated. 
Description copied from interface: CacheLoader
Loads multiple values from secondary data storage in case they are not in cache.

Specified by:
loadAll in interface CacheLoader
Parameters:
keys - Collection of (CacheQuery) Keys to load by.
Returns:
Map of key-value pairs loaded from data storage.
Space API Implementation Example

        public 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 Example

        public 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;
        }


store

public void store(Object key,
                  Object value)
Deprecated. 
Store the specified values under the specified keys in the store. This method should support both create and update for the specified key.

Specified by:
store in interface CacheStore
Parameters:
key - key to store the value under
value - value to be stored

storeAll

public void storeAll(Map map)
Deprecated. 
Store the specified values under the specified keys in the underlying store. This method is intended to support both key/value creation and value update for the specified keys.

Specified by:
storeAll in interface CacheStore
Parameters:
map - a Map of any number of keys and values to store

erase

public void erase(Object key)
Deprecated. 
Remove the specified key from the underlying store if present.

Specified by:
erase in interface CacheStore
Parameters:
key - key whose mapping is being removed from the cache

eraseAll

public void eraseAll(Collection keys)
Deprecated. 
Description copied from interface: CacheStore
Remove the specified keys from the underlying store if present

Specified by:
eraseAll in interface CacheStore
Parameters:
keys - keys whose mappings are being removed from the cache

executeBulk

public void executeBulk(List<BulkItem> bulk)
                 throws DataSourceException
Deprecated. 
This method is called in the following scenarios:

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

Specified by:
executeBulk in interface BulkDataPersister
Parameters:
bulk - This bulkSet contains set of BulkItem
Throws:
DataSourceException

initialLoad

public DataIterator<Map.Entry<Object,Object>> initialLoad()
                                                   throws DataSourceException
Deprecated. 
Description copied from interface: ManagedDataSource
Creates and returns an iterator over all the entries that should be loaded into space.

Specified by:
initialLoad in interface ManagedDataSource<Map.Entry<Object,Object>>
Returns:
a DataIterator or null if no data should be loaded into space
Throws:
DataSourceException

entries

public DataIterator<Map.Entry<Object,Object>> entries()
                                               throws DataSourceException
Deprecated. 
Description copied from interface: BasicCacheLoader
Creates and returns an iterator over all the entries in data source. Invoked on values() method

Specified by:
entries in interface BasicCacheLoader<Object,Object>
Returns:
a DataIterator or null if no data should be loaded into space
Throws:
DataSourceException