It appears that is a best practice to put all the database calls into a transaction. So, I wanted to put a select action in a transaction, but I can’t find how to do this.
I have tried this code, but I get an error:
using (var session = GetSession().SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
// var session = GetSession();
var result = session.Query<I>().Where(condition);
transaction.Commit();
return result;
}
Error:
Session is closed!
Object name: ‘ISession’.
It’s not a matter of
Transactionitself, although I only use transactions for save/update calls, not selects, but that might be a matter of preference (or I simply don’t know something important).The thing is, you’re not ‘materializing’ the collection before closing the session.
This should work:
The
Wheredoes not do anything by itself. Which means you’re just deferring execution of the filter until you do something with it – e.g. iterate over it. If you’re out of Session scope by then (and it seems you are), you’ll get the exception, since you can’t call the database when the session is closed.Although you probably won’t be able to access lazily loaded child items without eagerly
Fetching them first – you can’t call database through proxy when you’re not inside an open session. 🙂Disclaimer
By the way, same thing would happen in EF with LINQ: