Think of the Hibernate Contextual Session as mapping your current session to user context.
For example: one transaction can be such a context, therefore, if the life cycle of a Hibernate session matches the life of this transaction, a session can be called contextual when one transaction defines such a context . Sometimes this particular case is marked as a session-per-request model.
The Hibernate CurrentSessionContext interface is designed to map the current session (for example, SessionFactory.getCurrentSession() ) to various contexts. This interface has 3 implementations:
JTASessionContext : Current sessions are monitored and processed by a JTA transaction. The handling here is exactly the same as in the earlier JTA approach. See Javadocs for more details.
ThreadLocalSessionContext : Current sessions are tracked by the thread of execution. See Javadocs for more details.
ManagedSessionContext : Current sessions are tracked by the thread of execution. However, it is your responsibility to bind and untie the session instance with static methods in this class: it does not open, hide, or close the session
Take a look at the “Current Session Architecture” in the Hibernate documentation for more “official” details.
source share