spring-framework / org.springframework.orm.hibernate5 / HibernateTransactionManager / setHibernateManagedSession

setHibernateManagedSession

open fun setHibernateManagedSession(hibernateManagedSession: Boolean): Unit

Set whether to operate on a Hibernate-managed Session instead of a Spring-managed Session, that is, whether to obtain the Session through Hibernate's SessionFactory#getCurrentSession() instead of SessionFactory#openSession() (with a Spring TransactionSynchronizationManager check preceding it).

Default is "false", i.e. using a Spring-managed Session: taking the current thread-bound Session if available (e.g. in an Open-Session-in-View scenario), creating a new Session for the current transaction otherwise.

Switch this flag to "true" in order to enforce use of a Hibernate-managed Session. Note that this requires SessionFactory#getCurrentSession() to always return a proper Session when called for a Spring-managed transaction; transaction begin will fail if the getCurrentSession() call fails.

This mode will typically be used in combination with a custom Hibernate org.hibernate.context.spi.CurrentSessionContext implementation that stores Sessions in a place other than Spring's TransactionSynchronizationManager. It may also be used in combination with Spring's Open-Session-in-View support (using Spring's default SpringSessionContext), in which case it subtly differs from the Spring-managed Session mode: The pre-bound Session will not receive a clear() call (on rollback) or a disconnect() call (on transaction completion) in such a scenario; this is rather left up to a custom CurrentSessionContext implementation (if desired).