Search code examples
castle-activerecord

Batch save in CastleProject ActiveRecord


I need to save thousand of records in a database. I am using CastleProject ActiveRecord. The cycle which stores that amount of objects works too long.

Is it possible to run saving in a batch using ActiveRecord? What is recommended way to improve performance?


Solution

  • It seems I found the solution. There are two main steps:

    1. Add a batching to config file: <add key="hibernate.batch_size" value="100" />
    2. Use TransactionScope around saving your objects:

    using (TransactionScope scope = new TransactionScope())
    {
      for (int i = 0; i < 100; i++)
      {
        Contact contact = new Contact();
        contact.Save();
      }
    }