techsuri
techsuri

Reputation: 441

What is contextual sessions in hibernate?

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

Upvotes: 6

Views: 11815

Answers (2)

tolitius
tolitius

Reputation: 22499

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.

Upvotes: 9

Jyotirup
Jyotirup

Reputation: 2920

Another very good link explaining the concept of Hibernate Contextual Session

http://relation.to/2037.lace

Upvotes: 1

Related Questions