Lifecycle of Session


Session Factory Object, Lifecycle of Session

To establish a physical database connection, you must first establish a session. The Session object is designed to be lightweight and instantiated when a database interaction is required. Using a Session object, persistent objects are saved and retrieved.

Because session objects are not thread-safe, they should be generated and discarded as needed. Session instances can be created, read, and deleted.

At any given moment, instances may be in one of the following three states:

  • transient − It is deemed temporary by Hibernate if it is not associated with a Session, does not have a corresponding entry in the database, and does not have an identifier assigned to it.
  • persistent −By linking a transitory instance with a Session, you can make it continuous. Consistent instances have an associated Session and are stored in the database with an identifying value.
  • detached − persistent object becomes disconnected when we close the Hibernate Session.