- What is contextual session in Hibernate?
- When I created and closed a sessions in between use contextual session implementation interfaces?

- 11,070
- 14
- 66
- 115

- 431
- 3
- 5
- 10
2 Answers
Think of Hibernate's Contextual Session
as a mapping of a current Session to a user's Context.
For example: a single transaction could be such a context, hence if the Hibernate Session's lifecycle matches a life of this transaction, the Session could be called contextual, where a single transaction defines such a context
. Sometimes this particular case is labeled as a session-per-request
model.
A Hibernate interface CurrentSessionContext is there to map a current session ( e.g. SessionFactory.getCurrentSession()
) to different contexts. This interface has 3 implementations:
JTASessionContext: current sessions are tracked and scoped by a JTA transaction. The processing here is exactly the same as in the older JTA-only approach. See the Javadocs for details.
ThreadLocalSessionContext: current sessions are tracked by thread of execution. See the Javadocs for details.
ManagedSessionContext: current sessions are tracked by thread of execution. However, you are responsible to bind and unbind a Session instance with static methods on this class: it does not open, flush, or close a Session
Take a look at the Architecture Current Session part of the Hibernate documentation for more "official" details.

- 170,088
- 45
- 397
- 571

- 22,149
- 6
- 70
- 81
-
you should link to the official api doc, and to the latest reference manual. – JB Nizet Oct 24 '11 at 06:39
Another very good link explaining the concept of Hibernate Contextual Session

- 2,882
- 9
- 30
- 38