6
  1. What is contextual session in Hibernate?
  2. When I created and closed a sessions in between use contextual session implementation interfaces?
frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
techsuri
  • 431
  • 3
  • 5
  • 10

2 Answers2

9

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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
tolitius
  • 22,149
  • 6
  • 70
  • 81
1

Another very good link explaining the concept of Hibernate Contextual Session

http://relation.to/2037.lace

Jyotirup
  • 2,882
  • 9
  • 30
  • 38