hibernate/hibernate-orm · error · HibernateException

No session currently bound to execution context

Error message

No session currently bound to execution context

What it means

ManagedSessionContext stores sessions in a map keyed by session factory and — unlike thread/JTA contexts — never creates sessions itself: external code must bind one with ManagedSessionContext.bind(session). currentSession() throws this HibernateException when nothing is bound (never bound, already unbound, or the binder's scoping missed the current execution path).

Source

Thrown at hibernate-core/src/main/java/org/hibernate/context/internal/ManagedSessionContext.java:57

 * @author Steve Ebersole
 */
public class ManagedSessionContext extends AbstractCurrentSessionContext {
	private static final ThreadLocal<Map<SessionFactory,Session>> CONTEXT_TL = new ThreadLocal<>();

	/**
	 * Constructs a new ManagedSessionContext
	 *
	 * @param factory The factory this context will service
	 */
	public ManagedSessionContext(SessionFactoryImplementor factory) {
		super( factory );
	}

	@Override
	public Session currentSession() {
		final var current = existingSession( factory() );
		if ( current == null ) {
			throw new HibernateException( "No session currently bound to execution context" );
		}
		else {
			validateExistingSession( current );
			return current;
		}
	}

	/**
	 * Check to see if there is already a session associated with the current
	 * thread for the given session factory.
	 *
	 * @param factory The factory against which to check for a given session
	 * within the current thread.
	 * @return True if there is currently a session bound.
	 */
	public static boolean hasBind(SessionFactory factory) {
		return existingSession( factory ) != null;
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Bind a session before use: ManagedSessionContext.bind(factory.openSession()) and unbind + close in a finally block
  2. If automatic per-thread sessions were intended, use hibernate.current_session_context_class=thread instead
  3. In Spring, use the Spring-provided context (let the framework configure it) rather than 'managed' hand-wiring
  4. Centralize bind/unbind in one filter/interceptor so no code path bypasses it

Example fix

// before
Session s = sessionFactory.getCurrentSession(); // managed context, nothing bound

// after
Session s = sessionFactory.openSession();
ManagedSessionContext.bind(s);
try {
    Session current = sessionFactory.getCurrentSession(); // == s
    ...
} finally {
    ManagedSessionContext.unbind(sessionFactory);
    s.close();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!org.hibernate.context.internal.ManagedSessionContext.hasBind(sessionFactory)) {
    throw new IllegalStateException("No session bound; call ManagedSessionContext.bind() on the request path before getCurrentSession()");
}
Session s = sessionFactory.getCurrentSession();

Try / catch

try {
    return sessionFactory.getCurrentSession();
} catch (org.hibernate.HibernateException e) {
    if (e.getMessage().contains("No session currently bound")) {
        Session s = sessionFactory.openSession();
        org.hibernate.context.internal.ManagedSessionContext.bind(s); // bind-then-retry at one well-known place
        return s;
    }
    throw e;
}

Prevention

When it happens

Trigger: hibernate.current_session_context_class=managed and getCurrentSession() called before any bind(), after unbind() ran (request cleanup, error path), or from a thread/context the owning filter-interceptor did not cover.

Common situations: SE apps or OpenSessionInView-style clones choosing 'managed' without implementing the bind/unbind layer; cleanup code unbinding too early on error paths; switching from thread to managed without adding the binding infrastructure; async processing escaping the request scope that owns the binding.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/dd56357946de7b53. Report an issue: GitHub.