The reasons for not using a global session can be summarized as shown below:
* First level cache: you must understand. The first level cache is not limited to memory only. The consequence of the first level cache is that when an object is ever stored or deleted or requested, (n) hibernate must ensure that before this event the database must be in a consistent state with your memory. This is redness. However, (n) sleep mode has one unique feature called Transparent Persistence. Unlike other ORM structures, such as an entity, you do not know what is dirty. NHiberante does it for you. But the way this works is somewhat expensive. He compares all previous states of the object with new ones and tries to determine what has changed. Therefore, if your first level cache is full of entities, performance will degrade. There are two ways around this problem:
1-) using session.Flush (); session.Clear ();
2-) using a session without saving.
In the first case, all your pending changes go to the database. After that, you can safely clear the session. However, it stores the data in memory, even if your session is cleared until the transaction is deleted. This is because you can vote for a transaction that will be rejected at the end. (May provide additional information about this if required)
The second case, for a stateless session, (n) sleep mode behaves like a micro ORM. No tracking. It is lightweight but gives you more responsibility.
* Session-related exceptions: Another important reason why an applicationโs broadcast session is not used is whenever an exception is raised that is associated with a session or database (for example, a unique violation of restrictions), your session is doomed. You cannot work with it anymore because it is in a conflicting state with the database. Therefore, your global session must be updated, and this brings more complications.
* Stream Security: No session, nor any ADO.NET construct, is thread safe. If you use a global session object with multiple threads, you need to provide some thread safety. It can be very difficult.
Onur gumus
source share