Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What is hibernate session?


Asked by Cassandra Goodman on Dec 05, 2021 Hibernate



A Hibernate Session is just a transactional write-behind cache that translate entity state transitions into DML statements. The Hibernate Session can be connected or disconnected from the database.
In addition,
SessionFactory is also thread safe so only one thread can execute at a time its code. The instance of sessionFactory is heavyweighted because it contains connection, hibernate configuration, mapping files, location path so if you create number of instance of sessionFactory then your code becomes very heavy.
Likewise, Hibernate - Sessions. 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. Persistent objects are saved and retrieved through a Session object.
Indeed,
NHibernate session is a gateway for all operations that you perform on your datastore . It also plays a role of the first-level cache, minimizing the number of roundtrips between your application and the database server.
Keeping this in consideration,
SessionFactory is Hibernate's concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup.