Domanda

Do we have any other differences other than the below? Also please validate whether the below are correct

  1. SessionFactory objects are one per application and Session objects are one per client.
  2. SessionFactory is to create and manage Sessions. Session is to provide a CRUD interface for mapped classes, and also access to the more versatile Criteria API.
  3. SessionFactory is thread safe where as Session is not thread safe
È stato utile?

Soluzione

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.

Altri suggerimenti

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 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:

  1. It is one instance per datasource/database.
  2. It is thread-safe.
  3. It is a heavyweight object, because it maintains data sources, mappings, hibernate configuration information etc.
  4. Sessionfactory will create and manage the sessions.
  5. If you have say, 4 datasources/databases, then you must create 4 session factory instances.
  6. sessionfactory is an immutable object and it will be created as a singleton while the server initializes itself.

Session:

  1. It is one instance per client/thread/one transaction.
  2. It is not thread-safe.
  3. It is lightweight.
  4. sessions will be opened using sessionfactory.openSession() and some database operations will be done finally session will be closed using session.close().

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).

The main difference is that "There will be only one session factory object per hibernate client application. Because the implementation class of SessionFactory interface is singleton Java class".

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top