XAP.NET 7.0.X Documentation > Back to Table of Contents
.NET Persistency NHibernate External Data Source Walkthrough
Summary: A walkthrough of a common scenario for using the GigaSpaces NHibernate External Data Source.
OverviewThis page demonstrates how to use the GigaSpaces .Net NHibernate External Data Source in a common scenario: a cluster topology with a mirror. This is asynchronous persistency, which means that the operation against the cluster members is persisted to the database in an asynchronous manner. Each cluster member uses the External Data Source in read-only mode. Therefore it only reads data from the External Data Source, and each write operation is replicated to the mirror space. The mirror uses the External Data Source interface in write mode, and delegates destructive space operations (write, update, take) to the database through the External Data Source implementation.
NHibernate Mapping and Configuration FilesNHibernate Session Factory Configuration FileNHibernate requires a session factory that creates new sessions over the database for each operation executed on it. You can create such a session factory, either with a configuration file or by code. This walkthrough demonstrates a simple configuration file for the session factory, over a MySQL database server into a database named dotnetpersistency. These parameters are configured in the Connection string property. <?xml version="1.0" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory> <property name="dialect">NHibernate.Dialect.MySQLDialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.MySQLDataDriver</property> <property name="connection.connection_string">Server=localhost;Database=dotnetpersistency;User ID=root;CharSet=utf8</property> <!--Disable the writing of all the SQL statments to the console--> <property name="show_SQL">false</property> <!--Disabled the validation of your persistent classes, allows using .Net properties and not getters and setters on your fields--> <property name="use_proxy_validator">false</property> <!--This will create the tables in the database for your persistent classes according to the mapping file.--> <!--If the tables are already created this will recreate them and clear the data!--> <property name="hbm2ddl.auto">create</property> </session-factory> </hibernate-configuration> NHibernate Mapping FileEach persistent class requires a mapping file that defines how to map the object to and from the database. This walkthrough shows a simple Person class and its corresponding mapping file.
Person Class
Our Person is defined in Assembly name entities. namespace Entities { public class Person { private string _name; private int _age; [SpaceID, SpaceRouting] public string Name { get { return _name; } set { _name = value; } } [SpaceProperty(NullValue = 0)] public int Age { get { return _age; } set { _age = value; } } public BasicPerson(string name, int age) { _name = name; _age = age; } public BasicPerson() { } } } NHibernate Mapping File <?xml version="1.0"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Entities" namespace="Entities"> <class name="Entities.Person" table="PERSON"> <id name="Name" column="Name" type="string"> <generator class="assigned"/> </id> <property name="Age" /> </class> </hibernate-mapping> Starting the Spaces with NHibernate External Data SourceThis walkthrough demonstrates how to start the spaces with the NHibernate External Data Source from code, using a cluster in a [partitioned-sync2backup] topology.
Starting a Cluster Member SpaceThe following code starts a cluster member space with the NHibernate External Data Source: //Create a new space configuration object that is used to start a space SpaceConfig spaceConfig = new SpaceConfig(); //Start a new ExternalDataSource config object spaceConfig.ExternalDataSourceConfig = new ExternalDataSourceConfig(); //Start a new instance of NHibernateExternalDataSource and attach it to the config spaceConfig.ExternalDataSourceConfig.Instance = new NHibernateExternalDataSource(); //Create custom properties that are required to build NHibernate session factory spaceConfig.ExternalDataSourceConfig.CustomProperties = new Dictionary<string, string>(); //Point to NHibernate session factory config file spaceConfig.ExternalDataSourceConfig.CustomProperties.Add(NHibernateExternalDataSource.NHibernateConfigProperty, "<NHibernate config file>"); //Optional - points to a directory that contains the NHibernate mapping files (hbm) spaceConfig.ExternalDataSourceConfig.CustomProperties.Add(NHibernateExternalDataSource.NHibernateHbmDirectory, "<NHibernate HBM files location>"); //Our cluster member should only use the External Data Source in read only mode since the //mirror persist the data to the database (Async persistency) spaceConfig.ExternalDataSourceConfig.Usage = Usage.ReadOnly //Add custom properties spaceConfig.CustomProperties = new Dictionary<string, string>(); //State the External Data Source is in All-In-Cache mode spaceConfig.CustomProperties.Add("space-config.engine.cache_policy", "1"); //We need to configure this properties to let the cluster be aware that //there's a central database and not a database per space spaceConfig.CustomProperties.Add("cluster-config.cache-loader.external-data-source", "true"); spaceConfig.CustomProperties.Add("cluster-config.cache-loader.central-data-source", "true"); //Creates a cluster info with the partitioned-sync2backup cluster schema, stating 2,1 topology, partition id 1 and not a backup space. spaceConfig.ClusterInfo = new ClusterInfo("partitioned-sync2backup", 1, null, 2, 1); //Starts the space with the External Data Source ISpaceProxy persistentSpace = SpaceProxyProviderFactory.Instance.FindSpace("/./mySpace?mirror=true", spaceConfig);
To start the other members of the cluster, simply change the ClusterInfo:
Starting the Mirror SpaceThe following code starts the mirror space with the NHibernate External Data Source: //Create a new space configuration object that is used to start a space SpaceConfig spaceConfig = new SpaceConfig(); //Start a new ExternalDataSource config object spaceConfig.ExternalDataSourceConfig = new ExternalDataSourceConfig(); //Start a new instance of NHibernateExternalDataSource and attach it to the config spaceConfig.ExternalDataSourceConfig.Instance = new NHibernateExternalDataSource(); //Create custom properties that are required to build NHibernate session factory spaceConfig.ExternalDataSourceConfig.CustomProperties = new Dictionary<string, string>(); //Point to NHibernate session factory config file spaceConfig.ExternalDataSourceConfig.CustomProperties.Add(NHibernateExternalDataSource.NHibernateConfigProperty, "[NHibernate config file]"); //Optional - points to a directory that contains the NHibernate mapping files (hbm) spaceConfig.ExternalDataSourceConfig.CustomProperties.Add(NHibernateExternalDataSource.NHibernateHbmDirectory, "[NHibernate HBM files location]"); //The mirror persist the data, there for its usage is ReadWrite //However, since its a mirror it will only write data to the External Data Source and will not read data from it. spaceConfig.ExternalDataSourceConfig.Usage = Usage.ReadWrite //Add custom properties spaceConfig.CustomProperties = new Dictionary<string, string>(); //State the External Data Source is in All-In-Cache mode spaceConfig.CustomProperties.Add("space-config.engine.cache_policy", "1"); //We need to configure this properties to let the cluster be aware that //there's a central database and not a database per space spaceConfig.CustomProperties.Add("cluster-config.cache-loader.external-data-source", "true"); spaceConfig.CustomProperties.Add("cluster-config.cache-loader.central-data-source", "true"); //Starts the space with the External Data Source ISpaceProxy persistentSpace = SpaceProxyProviderFactory.Instance.FindSpace("/./mirror-service?schema=mirror", spaceConfig); |
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |