Let’s say that we have 100 objects of type User and each user has a one-to-one reference. The batch size in NHibernate config is set to 50. So, if we delete all 100 objects NHibernate will fire 200 connections to the DB. I want to delete them with 4 connection with 50 queries in each connection. The code below is a copy/paste from my batch insert methods which works as expected:
public void BatchDelete(IEnumerable<T> entitiesToDelete)
{
int batchSzie = Session.GetSessionImplementation().Batcher.BatchSize;
int batchedInsertsCount = 0;
foreach (var entity in entitiesToDelete)
{
batchedInsertsCount++;
Session.Delete(entity);
if (batchedInsertsCount % batchSzie == 0)
{
Session.Flush();
Session.Clear();
}
}
}
EDIT: I forgot to mention that I am in a transaction.
First of all you are not setting the batch size for the session (session.BatchSize()) so it will use what ever is the default or what you have configured when sessionfactory is created. But more importantly there is no support for batch deletes. Even if you would set the session.BatchSize() correctly it has no effect (at least with NHibernate 3.2 and SQL Server 2008). Ayende has a blog post that shows how to solve the problem.