A Hibernate SessionFactory is the thread-safe, application-wide object that holds the parsed entity mappings, the configuration and the second-level cache, and that creates Session objects on demand. You build it once at startup, share it everywhere, and open a short-lived Session for each unit of work. Building it is expensive because Hibernate validates mappings and prepares SQL, so creating one per request is a common and costly mistake.
In Jakarta Persistence terms, SessionFactory extends EntityManagerFactory, and a Session extends EntityManager. Both APIs reach the same engine, and in Spring Boot you normally never touch either factory directly.
A SessionFactory is an immutable snapshot of everything Hibernate needs to talk to one database. Once built, it does not change.
It does not pool connections itself. The pool is a separate component that the SessionFactory calls through a ConnectionProvider, so tuning pool size is done in the pool's settings, not on the factory.
This example uses Hibernate ORM 6 or 7 with annotated classes and a hibernate.properties file for connection settings.
StandardServiceRegistryBuilder().build() reads hibernate.properties from the classpath. The older new Configuration().configure().buildSessionFactory() still works and loads hibernate.cfg.xml, but the MetadataSources route is the recommended native bootstrap and makes registry cleanup explicit.
A Session is a cheap, single-threaded object. Open one for each unit of work and close it afterwards. The inTransaction helper starts a transaction, commits on success and rolls back on an exception.
fromTransaction, fromSession and inTransaction are available in Hibernate 6.3 and later. On older versions, use try (Session session = sessionFactory.openSession()) and call beginTransaction() yourself.
| Aspect | SessionFactory | Session |
|---|---|---|
| Lifetime | Whole application | One unit of work |
| Thread safety | Thread-safe | Not thread-safe |
| Cost to create | High | Low |
| Holds | Mappings, config, second-level cache | First-level cache, persistence context |
| Instances | One per database | Many |
The first-level cache lives in the Session and is private to it. The second-level cache lives in the SessionFactory and is shared, but it is disabled until you enable it and choose a cache provider.
Because the factory owns shared state, several features are configured on it.
hibernate.cache.use_second_level_cache=true and a JCache-compatible provider, then mark entities with @Cache.sessionFactory.getStatistics() exposes query counts and cache hit ratios when hibernate.generate_statistics is true, which helps find N+1 queries.Call sessionFactory.close() when the application stops so that pooled connections and cache regions are released. SessionFactory implements AutoCloseable, so a try-with-resources block works in tests and small tools. Frameworks do this for you.
Typical mistakes are building a factory per request, sharing a Session between threads, and keeping a Session open for the lifetime of the application, which grows the persistence context until memory runs out.
Yes. It is immutable after creation and designed to be shared by all threads. The Session it creates is not thread-safe.
One per database, created at startup. Applications that talk to several databases need one factory for each.
Usually not. Spring Boot creates an EntityManagerFactory, and you can inject an EntityManager. You can unwrap the SessionFactory with entityManagerFactory.unwrap(SessionFactory.class) when you need Hibernate-specific features.
openSession() always returns a new session that you must close. getCurrentSession() returns a session bound to a context, such as the current transaction, and requires hibernate.current_session_context_class to be configured.
The SessionFactory is Hibernate's long-lived, thread-safe entry point. It holds mappings, configuration and the second-level cache, delegates connections to a pool, and produces short-lived Session objects. Build it once, share it, open sessions per unit of work, and close the factory on shutdown.