com.gigaspaces.datasource.hibernate
Class HibernateDataSource

java.lang.Object
  extended by com.gigaspaces.datasource.hibernate.HibernateDataSource
All Implemented Interfaces:
BulkDataPersister, DataPersister<Object>, DataProvider<Object>, ExternalDataSource<Object>, ManagedDataSource<Object>, SQLDataProvider<Object>

public class HibernateDataSource
extends Object
implements ExternalDataSource<Object>

 HibernateDataSource is a full Hibernate implementation of space external data source.
 Changing data - when space data is written/taken/removed/updated
 HibernateDataSource is responsible to perform the relevant changes at the external database.
 HibernateDataSource delegates these changes into external database via Hibernate 3.0 ORM.

 Reading data - when the space is queried about specific data and it is not  found in the space
 HibernateDataSource is called to load the data from the database.

 DataProvider is used for data read.
 DataPersister  is used for write/update/remove

 Bulk operations - in case of transactions and replication will invoke the BulkDataPersister methods for persisting data.
 Complex queries against the space - SQLQuery and ExternalEntry matching will invoke the SQLDataProvider methods.

 See the DataProvider, DataPersister, SQLDataProvider  and BulkDataPersister for full description of the methods mapping between the Space API and the HibernateDataSource methods.
Configuration To use the HibernateDataSource the space schema XML configuration file should include the following. <space-config> <persistent> <enabled>true</enabled> <StorageAdapterClass>com.gigaspaces.datasource.DataAdapter</StorageAdapterClass> </persistent> <external-data-source> <data-source-class>com.gigaspaces.datasource.hibernate.HibernateDataSource</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.HibernateDataSource space-config.external-data-source.init-properties-file=hibernate.properties When using the HibernateDataSource the space-config.external-data-source.init-properties the content of this file will be passed on init to the HibernateDataSource. 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

Constructor Summary
HibernateDataSource()
           
 
Method Summary
 int count(Object template)
          Count number of object in the data source that match given template.
 int count(SQLQuery<Object> query)
          Count number of objects in the data source that match given query
 void executeBulk(List<BulkItem> bulk)
          This method is called in the following scenarios: 1.
 void init(Properties properties)
          Initialize and configure the data source using given properties.
 DataIterator<Object> initialLoad()
           Creates and returns an iterator over all the entries that should be loaded into space.
 DataIterator<Object> iterator(Object template)
          Create an iterator over all objects that match the given template.
 DataIterator<Object> iterator(SQLQuery<Object> query)
           Create an iterator over all objects that match the given SQLQuery.
 Object read(Object template)
          Read one object that matches the given template.
 void remove(Object object)
          Remove the given object from the data store
 void removeBatch(List<Object> objects)
          Remove given objects from the data store.
 void setDataClass(Class dataClass)
           
 void setSessionFactory(org.hibernate.SessionFactory _sessionFactory)
          Injects a Hibernate session factory into the data source to be used
 void shutdown()
          Close the data source and clean any used resources.
 void update(Object object)
          Update the given object in the data store
 void updateBatch(List<Object> objects)
          Update given objects in the data store.
 void write(Object object)
          Write given new object to the data store
 void writeBatch(List<Object> objects)
          Write given new objects to the data store.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateDataSource

public HibernateDataSource()
Method Detail

init

public void init(Properties properties)
          throws DataSourceException
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<Object>
Parameters:
properties - - contains user defined param and Partition data
Throws:
DataSourceException

setSessionFactory

public void setSessionFactory(org.hibernate.SessionFactory _sessionFactory)
Injects a Hibernate session factory into the data source to be used


setDataClass

public void setDataClass(Class dataClass)

shutdown

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

Specified by:
shutdown in interface ManagedDataSource<Object>
Throws:
DataSourceException

read

public Object read(Object template)
            throws DataSourceException
Description copied from interface: DataProvider
Read one object that matches the given template.
Used by the space for read templates with UID.

Specified by:
read in interface DataProvider<Object>
Returns:
Throws:
DataSourceException

remove

public void remove(Object object)
            throws DataSourceException
Description copied from interface: DataPersister
Remove the given object from the data store

Specified by:
remove in interface DataPersister<Object>
Throws:
DataSourceException

update

public void update(Object object)
            throws DataSourceException
Description copied from interface: DataPersister
Update the given object in the data store

Specified by:
update in interface DataPersister<Object>
Throws:
DataSourceException

write

public void write(Object object)
           throws DataSourceException
Description copied from interface: DataPersister
Write given new object to the data store

Specified by:
write in interface DataPersister<Object>
Throws:
DataSourceException

writeBatch

public void writeBatch(List<Object> objects)
                throws DataSourceException
Description copied from interface: DataPersister
Write given new objects to the data store.

If the implementation uses transactions,
all the objects must be written in one transaction.

Specified by:
writeBatch in interface DataPersister<Object>
Throws:
DataSourceException

removeBatch

public void removeBatch(List<Object> objects)
                 throws DataSourceException
Description copied from interface: DataPersister
Remove given objects from the data store.

If the implementation uses transactions,
All the objects must be removed in one transaction.

This operation is not currently supported by the space.

Specified by:
removeBatch in interface DataPersister<Object>
Throws:
DataSourceException

updateBatch

public void updateBatch(List<Object> objects)
                 throws DataSourceException
Description copied from interface: DataPersister
Update given objects in the data store.

If the implementation uses transactions,
all the objects must be updated in one transaction.

This operation is not currently supported by the space.

Specified by:
updateBatch in interface DataPersister<Object>
Throws:
DataSourceException

executeBulk

public void executeBulk(List<BulkItem> bulk)
                 throws DataSourceException
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

count

public int count(Object template)
          throws DataSourceException
Description copied from interface: DataProvider
Count number of object in the data source that match given template.
Note: null value can be passed - in case of a null template.

Specified by:
count in interface DataProvider<Object>
Returns:
Throws:
DataSourceException

iterator

public DataIterator<Object> iterator(Object template)
                              throws DataSourceException
Description copied from interface: DataProvider
Create an iterator over all objects that match the given template.
Note: null value can be passed - in case of a null template or at initial space load

Specified by:
iterator in interface DataProvider<Object>
Returns:
a DataIterator or null if no data was found that match the given template
Throws:
DataSourceException

count

public int count(SQLQuery<Object> query)
          throws DataSourceException
Description copied from interface: SQLDataProvider
Count number of objects in the data source that match given query

Specified by:
count in interface SQLDataProvider<Object>
Returns:
Throws:
DataSourceException

iterator

public DataIterator<Object> iterator(SQLQuery<Object> query)
                              throws DataSourceException
Description copied from interface: SQLDataProvider
 Create an iterator over all objects that match the given SQLQuery.
 SQLQuery contains a string representation of SQL 'where' clause.
 The format matches the PreparedStatement format(field1 = ? and field2 > ? and ....).
 The prepared values are accessible by SQLQuery.getPreparedValues() method
 
 Example implementation with SQL PreparedStatement:
 
 
 PreparedStatement st = con.prepareStatement(query.getSelectAllQuery());
 
 int index = 0;
 
 if(query.hasPreparedValues())
 {
        for(Iterator iter = query.getPreparedValues().iterator();iter.hasNext())
        {
                st.setObject(++index,iter.next());
        }
 }
 
 ResultSet rs = st.executeQuery();
 
 return new ResultSetIterator(rs);
 
 

Specified by:
iterator in interface SQLDataProvider<Object>
Returns:
a DataIterator or null if no data was found that match the given template
Throws:
DataSourceException
See Also:
ResultSetIterator

initialLoad

public DataIterator<Object> initialLoad()
                                 throws DataSourceException
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<Object>
Returns:
a DataIterator or null if no data should be loaded into space
Throws:
DataSourceException