Hello,
I used NHibernate to Insert a Tree in SQL Server 2005 Database, but when I use Session.Save, it will be very slow
hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Server=PURVENTIS-DB;Initial Catalog=PurventisTest;Persist Security Info=True;User ID=xxxx;Password=xxxxxx;Pooling=False
</property>
<property name="adonet.batch_size">100</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
</session-factory>
</hibernate-configuration>
NodeDTO.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataBasePlugin" namespace="DataBasePlugin.ModelLibrary.DomainModel">
<class name="NodeDTO" table="NodeDTO">
<id name="Id" column="Id" >
<generator class="guid" />
</id>
<property name="Name" column="Name" />
<property name="FeatureName" column="Featurename" />
<!--<many-to-one name="Characteristic" column="Characteristic" class="CharacteristicDTO" />-->
<many-to-one name="Parent" column="Parent" class="NodeDTO" cascade="all" index ="Node_index"/>
<property name="Value" column="Value" />
<property name="Level" column="Level" index="NodeLevel_index" />
</class>
</hibernate-mapping>
Code:
public class NodeDTO
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string FeatureName { get; set; }
public virtual NodeDTO Parent { get; set; }
public virtual double Value { get; set; }
public virtual int Level { get; set; }
}
Code:
class DBConnectHelper
{
private static ISessionFactory SESSION_FACTORY;
private static ISessionFactory SessionFactory
{
get
{
if (SESSION_FACTORY == null)
{
var configuration = new Configuration();
configuration.Configure("hibernate.cfg.xml");
configuration.AddAssembly(typeof(NodeDTO).Assembly);
SESSION_FACTORY = configuration.BuildSessionFactory();
new SchemaExport(configuration).Execute(false, true, false);
}
return SESSION_FACTORY;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
Method to Call
Code:
public static void WriteBulkNodesInDb(List<NodeDTO> pNodeList)
{
using (var lSession = DBConnectHelper.OpenSession())
{
using (var lTransaction = lSession.BeginTransaction())
{
foreach (var lNodeDto in pNodeList)
{
lSession.Save(lNodeDto); //When I use this, it will take one Minute for only 576 Nodes to write in Database
}
lTransaction.Commit();
}
}
}
I make a List for about 576 Nodes, and send it to the Method WriteBulkNodesInDb, but it takes one minute to write, I don't know where is the Problem. I test it then with StatelessSession, it used about 22 Second. It is unusual.
Can Someone help me to resolve this Problem? Thanks a lot!!
ps: I have yesterday tested it once more with NHProfiler, it will be only 6 Second for 5000 Rows!!! but if I didn't use the Initialized function of NHProfiler, it tooks slow again! What's the matter with NHibernate???