what is the best approach to load a collection and update all items with NHibernate. The current code loads 50 objects and processes each in its own transaction (if 1 fail others are OK).
NH Profiler says that there are too many sql queries per session.
After all, what do you think about this code?
using (var session = sessionFactory.OpenSession())
{
var myCollection =
(from obj in session.Query<MyObject>()
select obj).Take(50);
foreach (var item in myCollection)
{
using (var tx = session.BeginTransaction())
{
try
{
// Do some stuff...
session.Update(item);
tx.Commit();
}
catch (Exception)
{
tx.Rollback();
}
}
}
}
From NHibernate: Streaming large result sets :
Basically, use a stateless session and batching. Read also: NHibernate Perf Tricks