简体   繁体   中英

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. 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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