title: NHibernate, Databinding to DataGridView, Lazy Loading, and Session managment - need advice
body: <p>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.</p>
  
  <p>For performance reasons, I set the convention DefaultLazy.Always() for all DB access.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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 <a href="https://stackoverflow.com/questions/2526675/commit-is-very-slow-in-my-nhibernate-sqlite-project">Commit is VERY slow in my NHibernate / SQLite project</a>).</p>
  
  <p>So, my question - is this a good approach, or am I looking at problems down the road?  </p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p><strong>Edit</strong></p>
  
  <p><strong>It's been more than a year since I asked this question...and it turns out that keeping a main session open for the lifetime of the app has indeed led to performance problems.</strong> </p>
  
  <p><strong>There seem to be a lot more NH users on SO these days - anyone want to suggest a better approach?</strong></p>