Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is contextual sessions in hibernate?

Tags:

hibernate

  1. What is contextual session in Hibernate?
  2. When I created and closed a sessions in between use contextual session implementation interfaces?
like image 238
techsuri Avatar asked Oct 24 '11 04:10

techsuri


People also ask

What are the Hibernate Session methods?

It offers various functions such as create, delete, get, update to operate on the database by using session methods that exist in four states namely: Transient, Persistent and Detached, Removed. Hibernate has created to serve this purpose. It smoothly connects the database to java language irrespective of any database.

What is the difference between Session and SessionFactory?

The SessionFactory is a thread safe object and used by all the threads of an application. A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database.

What is SessionFactory Session and transaction in Hibernate?

SessionFactory is an Interface which is present in org. hibernate package and it is used to create Session Object. It is immutable and thread-safe in nature. buildSessionFactory() method gathers the meta-data which is in the cfg Object. From cfg object it takes the JDBC information and create a JDBC Connection.

Can I reuse the Session in Hibernate?

Spring opens a new Hibernate Session at the beginning of the request. These Sessions are not necessarily connected to the database. Every time the application needs a Session, it will reuse the already existing one.


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.

like image 57
tolitius Avatar answered Sep 23 '22 17:09

tolitius


Another very good link explaining the concept of Hibernate Contextual Session

http://relation.to/2037.lace

like image 42
Jyotirup Avatar answered Sep 25 '22 17:09

Jyotirup