I'm sure you are wondering why we keep calling .Commit()
after we call .Save()
. Basically, .Commit()
closes our transaction
and synchronizes the cached objects with the database.
We'll talk more about caching later, but in its simplest configuration, NHibernate uses a first level cache (or the session cache) to store objects. When you first query an object from the database, it is placed into this cache.
If you haven't told NHibernate to update or delete an object from the database, and it has already been cached, then it will pull this object from the cache rather than round-tripping to the database, improving performance.
By calling .Commit()
, we let NHibernate know that we have updated the record(s) in the transaction and that it should persist them to the database.