According to this question and corresponding answers, it is highly recommended for NHibernate to use transactions even for reading operations. I'm not sure about when fetching data is happened. Imagine we have a simple reading operation from MSQL Server database:
var topics = _topicRepository.Read(0, 10);
where read method of topic repository simply returns enumeration of values:
public IEnumerable<Topic> Read(int beginIndex, int amount)
{
return _session.Query<Topic>().Skip(beginIndex).Take(amount);
}
As I understand, NHibernate won't extract values from database (according to lazy initialization) until we call ToList() or do any other manipulations with data. But what is going on when we wrap this Read call into transaction and fetch data by calling ToList() until transaction is commited?
transaction.Begin();
var topics = _topicRepository.Read(0, 10).ToList();
transaction.Commit();
Calling ToList() is going to make database request immediately (it means that NHibernate should run transaction, which will contain single read operation), than what is going to be commited? And won't it be something like new transaction inside transaction? Thanks in advance for explanation.