简体   繁体   English

NHibernate:在单个事务上多次提交

[英]NHibernate: Multiple commits on a single transaction

I want to iterate through a set of object and make changes and then commit those changes in groups because the amount of data could be very large. 我想遍历一组对象并进行更改,然后将这些更改分组提交,因为数据量可能非常大。 When i do this i am getting an ObjectDisposedException. 当我这样做时,我得到一个ObjectDisposedException。 Any suggestions for how to better handle this? 有关如何更好地处理此问题的任何建议?

using (ITransaction tx = Session.BeginTransaction())
{
    for (int i = 0; i < 100; i++)
    {
        //DO Something
        if (i % 10 == 0)
        {
            tx.Commit();
        }
    }
}

You are committing the transaction within a loop, meaning you are trying to commit one open transaction multiple times. 您正在循环内提​​交事务,这意味着您试图多次提交一个未完成的事务。 This is not possible by design. 设计上不可能做到这一点。 One transaction can only be committed once. 一笔交易只能提交一次。

So your two options are, have one transaction surrounding the loop 因此,您有两个选择,就是在循环中进行一次交易

or one transaction per loop. 或每个循环一个事务。 Of course you can still batch your changes every x number of objects, but per batch you'll have to open a new transaction and commit the changes, then open another one etc... 当然,您仍然可以每x个对象批处理更改,但是每批处理都必须打开一个新事务并提交更改,然后再打开一个对象,依此类推...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM