加快与NHibernate的批量插入操作批量、操作、NHibernate

2023-09-04 01:45:11 作者:我爱你像飞蛾扑火

我想加快散装插入操作与NHibernate 3.2的Oracle 11g。要做到这一点,我试过

I want to speed up bulk insert operations with NHibernate 3.2 on Oracle 11g. To do this I tried

Session.Save(entity);
Session.Flush();
Session.Clear();

...在我的的foreach 循环,但有引起对象的会话丢失的一个例外:

... in my foreach loop but got an exception caused by objects missing in the Session:

未能懒洋洋地初始化角色的集合:MyClass.PropertyX,无会话或会话关闭

failed to lazily initialize a collection of role: MyClass.PropertyX, no session or session was closed

另一种尝试是设置批量大小:

Another attempt was to set the batch size:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
    <property name="connection.connection_string">xxx</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="adonet.batch_size">50</property>
    <property name="query.substitutions">true=1, false=0</property>
    <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate</property>
  </session-factory>
</hibernate-configuration>

此外我设置 Session.SetBatchSize(50)在我的code时得到以下异常:

additionally I set Session.SetBatchSize(50) in my code an got the following exception:

没有批量大小被定义为会话工厂,配料是   禁用。设置adonet.batch_size = 1,使配料。

No batch size was defined for the session factory, batching is disabled. Set adonet.batch_size = 1 to enable batching.

如果这异常被抛出的唯一位置是NonBatchingBatcher,所以它看起来像我的会议有错误的配料。

The only location where this exception is thrown is NonBatchingBatcher, so it looks like my session has the wrong batcher.

什么是错在这里?我如何可以加快批量插入与NHibernate(不使用statlese会话)?

What is wrong here? How can I speed up batch inserts with NHibernate (without using statlese sessions)?

推荐答案

以下应该工作,

var testObjects = CreateTestObjects(500000);

var stopwatch = new Stopwatch();
stopwatch.Start();
using (IStatelessSession session = sessionFactory.OpenStatelessSession())
using (ITransaction transaction = session.BeginTransaction())
{
    foreach (var testObject in testObjects)
        session.Insert(testObject);
    transaction.Commit();
}

stopwatch.Stop();
var time = stopwatch.Elapsed;

编号:http://nhibernate.info/blog/2008/10/30/bulk-data-operations-with-nhibernate-s-stateless-sessions.html