Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What should you do when creating a nhibernate session?


Asked by Avery Jimenez on Dec 08, 2021 FAQ



You should observe the following practices when creating NHibernate Sessions: Never create more than one concurrent ISession or ITransaction instance per database connection. Be extremely careful when creating more than one ISession per database per transaction.
Moreover,
The NHibernate session encapsulates a unit of work as specified by the unit of work pattern. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.
Also, In NHibernate we have the Session object which is a Unit of Work (UoW) container. The session object keeps track of all objects you load, new objects you add, existing objects you delete and changes you make to any of these objects.
In this manner,
The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method.
Thereof,
This approach leaves NHibernate and the database to worry about concurrency. The application never needs to synchronize on any business object, as long as it sticks to a single thread per ISession or object identity (within an ISession the application may safely use == to compare objects). 12.4. Optimistic concurrency control