问题描述
我想在 Oracle 11g 上使用 NHibernate 3.2 加速批量 insert
操作.为此,我尝试过
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,没有会话或会话被关闭
另一个尝试是设置批量大小:
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)
并得到以下异常:
additionally I set Session.SetBatchSize(50)
in my code an got the following exception:
没有为会话工厂定义批处理大小,批处理是禁用.设置 adonet.batch_size = 1 以启用批处理.
引发此异常的唯一位置是 NonBatchingBatcher,所以看起来我的会话有错误的批处理器.
The only location where this exception is thrown is NonBatchingBatcher, so it looks like my session has the wrong batcher.
这里有什么问题?如何使用 NHibernate 加速批量插入(不使用状态会话)?
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;
这篇关于使用 NHibernate 加速批量插入操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!