nhibernate:批量执行更新 [英] nhibernate : executing updates in batches

查看:203
本文介绍了nhibernate:批量执行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NHibernate进行批量更新,但是它没有在进行批量更新,而是对所有行进行单独写入.我必须向数据库写大约1万行.

I am trying to do batch updates using NHibernate, but it is not doing batch updates, its doing individual writes for all the rows. I have to write around 10k rows to db.

        using (var session = GetSessionFactory().OpenStatelessSession())
        {
            session.SetBatchSize(100);
            using (var tx = session.BeginTransaction())
            {
                foreach (var pincode in list)
                {
                    session.Update(pincode);
                }
                tx.Commit();
            }
        }

我尝试使用session.SetBatchSize(100);将批次大小设置为100,但这无济于事.还尝试使用cfg.SetProperty("adonet.batch_size", "100");设置批次大小,但这也无济于事.

I am tried setting batch size to 100 using session.SetBatchSize(100); but that does not help. Also tried setting batch size using cfg.SetProperty("adonet.batch_size", "100"); but thats also not helping.

我正在使用GUID主键,因此我不理解批量更新失败的原因. 此处.但这对我不起作用.

I am using GUID primary keys, hence I dont understand the reason for batch update failure. This is exactly the solution explained here. But its not working for me.

注意,我在所有实体上都有用于乐观并发的版本字段.难道是没有批量更新的罪魁祸首?

NOTE I have version field for optimistic concurrency mapped on all the entities. can that be the culprit for not having batch updates??

编辑

我尝试使用有状态会话,但这也无济于事

i tried using state-ful session but that also did not help

        //example 2
        using (var session = GetSessionFactory().OpenSession())
        {
            session.SetBatchSize(100);
            session.FlushMode = FlushMode.Commit;
            foreach (var pincode in list)
            {
                session.Update(pincode);
            }
            session.Flush();
        }

       //example 3
        using (var session = GetSessionFactory().OpenSession())
        {
            session.SetBatchSize(100);
            using (var tx = session.BeginTransaction())
            {
                foreach (var pincode in list)
                {
                    session.Update(pincode);
                }
                tx.Commit();
            }
        }

example 2由于某种原因导致两次往返旅行.

example 2 for some reason is causing double round trips.

编辑

经过进一步研究,我发现每个会话.更新实际上是在更新数据库

after further research I found that, each session.Update is actually updating the db

        using (var session = SessionManager.GetStatelessSession())
        {
            session.SetBatchSize(100);
            foreach (var record in list)
            {
                session.Update(record);
            }
        }

如何避免这种情况.

编辑

也尝试过刷新模式,但这也无济于事

tried with flush mode as well, but thats also not helping

        using (var session = SessionManager.GetNewSession())
        {
            session.FlushMode = FlushMode.Never;
            session.SetBatchSize(100);
            session.BeginTransaction();
            foreach (var pincode in list)
            {
                session.SaveOrUpdate(pincode);
            }
            session.Flush();
            session.Transaction.Commit();
        }

编辑4

考虑到我要在同一会话中获取所有实体并仅在该会话中更新和保存它们,因此即使低于一个也不起作用.

even below one is not working, given i am fetching all entities in same session and updating and saving them in that session only...

        using (var session = SessionManager.GetSessionFactory().OpenSession())
        {
            session.SetBatchSize(100);
            session.FlushMode = FlushMode.Commit;
            session.Transaction.Begin();
            var list = session.QueryOver<Pincode>().Take(1000).List();
            list.ForEach(x => x.Area = "Abcd" + DateTime.Now.ToString("HHmmssfff"));
            foreach (var pincode in list) session.SaveOrUpdate(pincode);
            session.Flush();
            session.Transaction.Commit();
        }

推荐答案

nhibernate 不会批量处理版本化的实体,这是我的问题.

nhibernate does not batch versioned entities that was the issue in my case.

您无法批量处理实体版本,唯一要做的就是使该实体不被版本控制.

There is no way you can batch version entities, the only to do this is to make the entity non versioned.

这篇关于nhibernate:批量执行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆