What is the pros and cons using IStatelessSession
over ISession
in NHibernate?
Asked
Active
Viewed 7,000 times
17

Fitzchak Yitzchaki
- 9,095
- 12
- 56
- 96
1 Answers
21
StatelessSession doesn't tracks changes made to the entities and has no lazy loading support. Thats why it has a better performance than Session.
But with stateless session you are forced to manage Insert, Delete, Update operations manually and this really uncomfortable.
Stateless session is made for batch operations where you need to make a lots of queries to database and you don't need tracking changes, etc.

Sly
- 15,046
- 12
- 60
- 89
-
There are different use cases as well. For example, when you have small database and want to keep it all in memory for the entire lifetime of the application, and need the database only to be able restore the state of the application after restarting it. In that case, `IStatelessSession` saves you from the performance overhead associated with lazy loading, merging etc.... of course that also means that you will have to implement some sort of change tracking yourself, e.g. with a custom Unit of Work pattern implementation. Of course, NHibernate may not be the best fit for that use case ... – chris Jul 06 '15 at 08:26