hibernate/hibernate-orm · error · HibernateException

No CurrentSessionContext configured

Error message

No CurrentSessionContext configured

What it means

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().

Source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java:709

		return defaultSessionOpenOptions != null
				? defaultSessionOpenOptions.openSession()
				: withOptions().openSession();
	}

	@Override
	public SessionImplementor openTemporarySession() {
		// The temporarySessionOpenOptions can't be used in some cases;
		// for example when using a TenantIdentifierResolver.
		return temporarySessionOpenOptions != null
				? temporarySessionOpenOptions.openSession()
				: buildTemporarySessionOpenOptions().openSession();
	}

	@Override
	@Nonnull
	public Session getCurrentSession() {
		if ( currentSessionContext == null ) {
			throw new HibernateException( "No CurrentSessionContext configured" );
		}
		return currentSessionContext.currentSession();
	}

	@Override
	@Nonnull
	public SessionBuilderImplementor withOptions() {
		return sessionBuilder( true );
	}

	private SessionBuilderImplementor sessionBuilder(boolean notifyLifecycleCallbacks) {
		return new SessionBuilderImpl( this ) {
			@Override
			protected SessionImplementor createSession(StatefulOptions options) {
				final var session = new SessionImpl( SessionFactoryImpl.this, options );
				if ( notifyLifecycleCallbacks ) {
					postCreate( session );
				}

View on GitHub (pinned to fad1729dce)

Solutions

  1. 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
  2. In Spring applications, obtain the EMF from Spring (LocalContainerEntityManagerFactoryBean) and use EntityManager injection or getCurrentEntityManager() instead of Hibernate's getCurrentSession()
  3. If you do not want ambient-session semantics, replace getCurrentSession() calls with sessionFactory.openSession() and manage closing yourself
  4. With JTA, verify the transaction coordinator is actually JTA (WildFly/WebSphere) because the JTA context is auto-registered only then

Example fix

// before
Session s = sessionFactory.getCurrentSession(); // HibernateException: No CurrentSessionContext configured

// after
Map<String, Object> cfg = new HashMap<>();
cfg.put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
SessionFactory sessionFactory = new Configuration()
        .addProperties(cfg)
        .buildSessionFactory();
Session s = sessionFactory.getCurrentSession(); // now resolves the thread-bound session
Defensive patterns

Strategy: validation

Validate before calling

Map<String, Object> props = emf.getProperties();
boolean hasCurrentSessionContext =
        props.containsKey("hibernate.current_session_context_class")
        || "JTA".equals(String.valueOf(props.get("jakarta.persistence.transactionType")));
Session session = hasCurrentSessionContext
        ? sessionFactory.getCurrentSession()
        : sessionFactory.openSession();

Try / catch

try {
    return sessionFactory.getCurrentSession();
} catch (HibernateException e) {
    if (e.getMessage().contains("No CurrentSessionContext")) {
        throw new IllegalStateException(
            "Configure hibernate.current_session_context_class (thread/jta/managed) or use openSession()", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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