NHibernate, Databinding to DataGridView, Lazy Loading, and Session managment - need advice

Posted by Tom Bushell on Stack Overflow See other posts from Stack Overflow or by Tom Bushell
Published on 2010-04-02T16:05:35Z Indexed on 2010/04/03 19:33 UTC
Read the original article Hit count: 856

My main application form (WinForms) has a DataGridView, that uses DataBinding and Fluent NHibernate to display data from a SQLite database. This form is open for the entire time the application is running.

For performance reasons, I set the convention DefaultLazy.Always() for all DB access.

So far, the only way I've found to make this work is to keep a Session (let's call it MainSession) open all the time for the main form, so NHibernate can lazy load new data as the user navigates with the grid.

Another part of the application can run in the background, and Save to the DB. Currently, (after considerable struggle), my approach is to call MainSession.Disconnect(), create a disposable Session for each Save, and MainSession.Reconnect() after finishing the Save. Otherwise SQLite will throw "The database file is locked" exceptions.

This seems to be working well so far, but past experience has made me nervous about keeping a session open for a long time (I ran into performance problems when I tried to use a single session for both Saves and Loads - the cache filled up, and bogged down everything - see http://stackoverflow.com/questions/2526675/commit-is-very-slow-in-my-nhibernate-sqlite-project).

So, my question - is this a good approach, or am I looking at problems down the road?

If it's a bad approach, what are the alternatives? I've considered opening and closing my main session whenever the user navigates with the grid, but it's not obvious to me how I would do that - hook every event from the grid that could possibly cause a lazy load?

I have the nagging feeling that trying to manage my own sessions this way is fundamentally the wrong approach, but it's not obvious what the right one is.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about session