{"record":{"id":"dd56357946de7b53","repo":"hibernate/hibernate-orm","slug":"no-session-currently-bound-to-execution-context","errorCode":null,"errorMessage":"No session currently bound to execution context","messagePattern":"No session currently bound to execution context","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/context/internal/ManagedSessionContext.java","lineNumber":57,"sourceCode":" * @author Steve Ebersole\n */\npublic class ManagedSessionContext extends AbstractCurrentSessionContext {\n\tprivate static final ThreadLocal<Map<SessionFactory,Session>> CONTEXT_TL = new ThreadLocal<>();\n\n\t/**\n\t * Constructs a new ManagedSessionContext\n\t *\n\t * @param factory The factory this context will service\n\t */\n\tpublic ManagedSessionContext(SessionFactoryImplementor factory) {\n\t\tsuper( factory );\n\t}\n\n\t@Override\n\tpublic Session currentSession() {\n\t\tfinal var current = existingSession( factory() );\n\t\tif ( current == null ) {\n\t\t\tthrow new HibernateException( \"No session currently bound to execution context\" );\n\t\t}\n\t\telse {\n\t\t\tvalidateExistingSession( current );\n\t\t\treturn current;\n\t\t}\n\t}\n\n\t/**\n\t * Check to see if there is already a session associated with the current\n\t * thread for the given session factory.\n\t *\n\t * @param factory The factory against which to check for a given session\n\t * within the current thread.\n\t * @return True if there is currently a session bound.\n\t */\n\tpublic static boolean hasBind(SessionFactory factory) {\n\t\treturn existingSession( factory ) != null;\n\t}","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/context/internal/ManagedSessionContext.java#L39-L75","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Bind a session before use: ManagedSessionContext.bind(factory.openSession()) and unbind + close in a finally block","If automatic per-thread sessions were intended, use hibernate.current_session_context_class=thread instead","In Spring, use the Spring-provided context (let the framework configure it) rather than 'managed' hand-wiring","Centralize bind/unbind in one filter/interceptor so no code path bypasses it"],"exampleFix":"// before\nSession s = sessionFactory.getCurrentSession(); // managed context, nothing bound\n\n// after\nSession s = sessionFactory.openSession();\nManagedSessionContext.bind(s);\ntry {\n    Session current = sessionFactory.getCurrentSession(); // == s\n    ...\n} finally {\n    ManagedSessionContext.unbind(sessionFactory);\n    s.close();\n}","handlingStrategy":"validation","validationCode":"if (!org.hibernate.context.internal.ManagedSessionContext.hasBind(sessionFactory)) {\n    throw new IllegalStateException(\"No session bound; call ManagedSessionContext.bind() on the request path before getCurrentSession()\");\n}\nSession s = sessionFactory.getCurrentSession();","typeGuard":null,"tryCatchPattern":"try {\n    return sessionFactory.getCurrentSession();\n} catch (org.hibernate.HibernateException e) {\n    if (e.getMessage().contains(\"No session currently bound\")) {\n        Session s = sessionFactory.openSession();\n        org.hibernate.context.internal.ManagedSessionContext.bind(s); // bind-then-retry at one well-known place\n        return s;\n    }\n    throw e;\n}","preventionTips":["Centralize bind/unbind(+close) in a single filter/interceptor with finally","Prefer 'thread' or framework-provided contexts unless you truly manage sessions manually","Guard async paths: check hasBind() before touching getCurrentSession() off-request"],"tags":["hibernate","session-management","managed-session-context","configuration","current-session"],"backgroundTag":"session-not-bound-to-context","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}