Do we have any other differences other than the below? Also please validate whether the below are correct
SessionFactory
objects are one per applicati
Session Factory can be considered as the factory which will yield us the sessions for a particular datasource or database. In other words, if our application has more than one database, then we should create as many session factories as are our number of databases. So, Session factory is long-lived.
Session is the short lived instance used to perform discrete transactions to the database. Usually, at any time, if a transaction is required to be done with the database, a short lived session object is obtained from the appropriate Session Factory instance and after the transaction is completed, the instance is no more available.
First of all, asking the difference between these interfaces doesn't make any sense. It seems like asking the difference between car manufacturing plant and car. A manufacturing plant is a place where the cars will be produced. Similarly, SessionFactory
is an instance which will create Session
objects.
SessionFactory is an interface. SessionFactory can be created by providing Configuration object, which will contain all DB related property details pulled from either hibernate.cfg.xml file or hibernate.properties file. SessionFactory is a factory for Session objects.
We can create one SessionFactory implementation per database in any application. If your application is referring to multiple databases, then you need to create one SessionFactory per database.
The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use. 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. Persistent objects are saved and retrieved through a Session object.
The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed. The main function of the Session is to offer, create, read, and delete operations for instances of mapped entity classes.
Sessionfactory:
Session:
SessionFactory
is a factory class for Session
objects. It is available for the whole application while a Session
is only available for particular transaction.
Session
is short-lived while SessionFactory
objects are long-lived. SessionFactory
provides a second level cache and Session
provides a first level cache.
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.
No, Session is not Thread Safe. A Session is a light weight and a non-threadsafe object (No, you cannot share it between threads) that represents a single unit-of-work with the database. Sessions are opened by a SessionFactory and then are closed when all work is complete. Session is the primary interface for the persistence service. A session obtains a database connection lazily (i.e. only when required).