Mic*_*cah 12 nhibernate ado.net batch-processing
我目前正在使用NHibernate.我有一种情况需要将一堆记录保存到数据库中,如下所示:
var relatedTopics = GetRelatedTopics(topic);
foreach (var relatedTopic in relatedTopics /* could be anywhere from 10 - 1000+ */)
{
var newRelatedTopic = new RelatedTopic { RelatedTopicUrl = relatedTopic, TopicUrl = topic.Name };
_repository.Save(newRelatedTopic);
}
Run Code Online (Sandbox Code Playgroud)
当有大量的记录需要保存时,这显然非常非常繁重,不得不多次访问数据库.什么是更好的方法?我可以做一些批量更新吗?我最好使用DataSet吗?
谢谢
Tob*_*orn 15
设置adonet.batch_size可以改善这种情况.
为此,你必须
例:
m_sessionFactory = Fluently
.Configure()
.Database(MsSqlConfiguration
.MsSql2005
.ConnectionString(c => c.FromConnectionStringWithKey("testme"))
)
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<TestImpl>())
.ExposeConfiguration(config =>
{
config.SetProperty("adonet.batch_size", "1");
m_configuration = config;
})
.BuildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
在保存之前的会话上设置批量大小
using (ISession session = m_nhibernateSessionFactory.GetSession())
using (var tx = session.BeginTransaction())
{
session.SetBatchSize(1000);
foreach (var server in serverz)
{
session.SaveOrUpdate(server);
}
tx.Commit();
}
Run Code Online (Sandbox Code Playgroud)我相信这就是你要找的东西:
基本上不是打开一个ISession,而是打开一个IStatelessSession,在你的hibernate.cfg.xml中你可以设置:
<property name="adonet.batch_size">100</property>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14674 次 |
| 最近记录: |