Stateless sessions
NHibernate sessions are stateful by default. In other words, they maintain the state of the entities that pass through them. During the course of execution of any functionality, following is what usually happens if you are using a NHibernate session:
- You open a new session.
- You begin a transaction on this session.
- You load some entities from the database using the session.
- The entities loaded are tracked in the identity map (session level cache), in case they are queried again.
- The entities are also kept in the second level cache if it is configured.
- You then update few properties on the entities.
- You then commit your NHibernate transaction to signal to NHibernate to persist your in-memory changes to the database (remember transitive persistence?).
- NHibernate uses dirty checking to find out which of the entities in the session have changed and only persists the changes to the database.
- As part of above, NHibernate also cascades the database operations down to the collections/associations...