gpt4 book ai didi

.net - 使用 NHibernate 加速批量插入操作

转载 作者:行者123 更新时间:2023-12-03 14:31:29 27 4
gpt4 key购买 nike

我要批量加速insert在 Oracle 11g 上使用 NHibernate 3.2 进行操作。为此,我试过

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

... 在我的 foreach循环但由于 session 中缺少对象而导致异常:

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



另一种尝试是设置批量大小:
<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)在我的代码中出现以下异常:

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



抛出此异常的唯一位置是 NonBatchingBatcher ,所以看起来我的 session 有错误的批处理器。

这里有什么问题?如何使用 NHibernate 加速批量插入(不使用状态 session )?

最佳答案

以下应该工作,

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

关于.net - 使用 NHibernate 加速批量插入操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9943967/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com