{"record":{"id":"627330c2c1fe47d8","repo":"hibernate/hibernate-orm","slug":"no-currentsessioncontext-configured","errorCode":null,"errorMessage":"No CurrentSessionContext configured","messagePattern":"No CurrentSessionContext configured","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java","lineNumber":709,"sourceCode":"\t\treturn defaultSessionOpenOptions != null\n\t\t\t\t? defaultSessionOpenOptions.openSession()\n\t\t\t\t: withOptions().openSession();\n\t}\n\n\t@Override\n\tpublic SessionImplementor openTemporarySession() {\n\t\t// The temporarySessionOpenOptions can't be used in some cases;\n\t\t// for example when using a TenantIdentifierResolver.\n\t\treturn temporarySessionOpenOptions != null\n\t\t\t\t? temporarySessionOpenOptions.openSession()\n\t\t\t\t: buildTemporarySessionOpenOptions().openSession();\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic Session getCurrentSession() {\n\t\tif ( currentSessionContext == null ) {\n\t\t\tthrow new HibernateException( \"No CurrentSessionContext configured\" );\n\t\t}\n\t\treturn currentSessionContext.currentSession();\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic SessionBuilderImplementor withOptions() {\n\t\treturn sessionBuilder( true );\n\t}\n\n\tprivate SessionBuilderImplementor sessionBuilder(boolean notifyLifecycleCallbacks) {\n\t\treturn new SessionBuilderImpl( this ) {\n\t\t\t@Override\n\t\t\tprotected SessionImplementor createSession(StatefulOptions options) {\n\t\t\t\tfinal var session = new SessionImpl( SessionFactoryImpl.this, options );\n\t\t\t\tif ( notifyLifecycleCallbacks ) {\n\t\t\t\t\tpostCreate( session );\n\t\t\t\t}","sourceCodeStart":691,"sourceCodeEnd":727,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java#L691-L727","documentation":"SessionFactoryImpl.getCurrentSession() delegates to a CurrentSessionContext, which Hibernate only installs when one is configured. If no current-session context exists (no hibernate.current_session_context_class setting and no programmatic context), the field is null and HibernateException is thrown. 'Current session' management (thread-bound, JTA-scoped, Spring-managed) is opt-in, unlike openSession().","triggerScenarios":"Calling sessionFactory.getCurrentSession() when the session factory was built without hibernate.current_session_context_class (or AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS) and without JTA transaction integration. Common when manually bootstrapping via StandardServiceRegistryBuilder in Java SE, or when code migrated from openSession() to getCurrentSession() without adding the config.","commonSituations":"Java SE apps with manual bootstrap (no Spring); switching a persistence unit from JTA to RESOURCE_LOCAL, which loses the implicit JTA current-session context; a typo in the property name or an invalid value so the context is never registered; Spring setups where the EMF is built manually instead of via LocalContainerEntityManagerFactoryBean, missing SpringSessionContext.","solutions":["Set hibernate.current_session_context_class to thread, jta, or managed (e.g. properties.put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, \"thread\")) before building the factory","In Spring applications, obtain the EMF from Spring (LocalContainerEntityManagerFactoryBean) and use EntityManager injection or getCurrentEntityManager() instead of Hibernate's getCurrentSession()","If you do not want ambient-session semantics, replace getCurrentSession() calls with sessionFactory.openSession() and manage closing yourself","With JTA, verify the transaction coordinator is actually JTA (WildFly/WebSphere) because the JTA context is auto-registered only then"],"exampleFix":"// before\nSession s = sessionFactory.getCurrentSession(); // HibernateException: No CurrentSessionContext configured\n\n// after\nMap<String, Object> cfg = new HashMap<>();\ncfg.put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, \"thread\");\nSessionFactory sessionFactory = new Configuration()\n        .addProperties(cfg)\n        .buildSessionFactory();\nSession s = sessionFactory.getCurrentSession(); // now resolves the thread-bound session","handlingStrategy":"validation","validationCode":"Map<String, Object> props = emf.getProperties();\nboolean hasCurrentSessionContext =\n        props.containsKey(\"hibernate.current_session_context_class\")\n        || \"JTA\".equals(String.valueOf(props.get(\"jakarta.persistence.transactionType\")));\nSession session = hasCurrentSessionContext\n        ? sessionFactory.getCurrentSession()\n        : sessionFactory.openSession();","typeGuard":null,"tryCatchPattern":"try {\n    return sessionFactory.getCurrentSession();\n} catch (HibernateException e) {\n    if (e.getMessage().contains(\"No CurrentSessionContext\")) {\n        throw new IllegalStateException(\n            \"Configure hibernate.current_session_context_class (thread/jta/managed) or use openSession()\", e);\n    }\n    throw e;\n}","preventionTips":["Fail fast at startup: assert getCurrentSession() works once right after building the factory","In Spring, never hand-call getCurrentSession() — inject EntityManager and let SpringSessionContext manage it","Standardize one session-acquisition pattern per codebase (ambient vs explicit open/close) to avoid mixed assumptions"],"tags":["hibernate","session","configuration","current-session","bootstrap"],"backgroundTag":"current-session-not-configured","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}