hibernate/hibernate-orm · error · IllegalStateException

No provided connection

Error message

No provided connection

What it means

IllegalStateException from LogicalConnectionProvidedImpl.getPhysicalConnection(): the session is open but its user-supplied connection is absent because session.disconnect() detached it earlier. Any JDBC work (query, flush, transaction begin) in this disconnected window fails — the session must be reconnected first.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionProvidedImpl.java:84

		}
		finally {
			providedConnection = null;
			closed = true;
			CONNECTION_LOGGER.logicalConnectionClosed();
		}
	}

	@Override
	public boolean isPhysicallyConnected() {
		return providedConnection != null;
	}

	@Override
	@Nonnull
	public Connection getPhysicalConnection() {
		errorIfClosed();
		if ( providedConnection == null ) {
			throw new IllegalStateException( "No provided connection" );
		}
		return providedConnection;
	}

	@Override
	public void serialize(ObjectOutputStream oos) throws IOException {
		oos.writeBoolean( closed );
		oos.writeBoolean( initiallyAutoCommit );
	}

	public static LogicalConnectionProvidedImpl deserialize(
			ObjectInputStream ois) throws IOException, ClassNotFoundException {
		final boolean isClosed = ois.readBoolean();
		final boolean initiallyAutoCommit = ois.readBoolean();
		return new LogicalConnectionProvidedImpl( isClosed, initiallyAutoCommit );
	}

	@Override

View on GitHub (pinned to fad1729dce)

Solutions

  1. Reconnect before use: session.reconnect(freshConnection) — only valid for user-supplied-connection sessions
  2. Prefer short-lived sessions opened per unit of work instead of the disconnect/reconnect pattern
  3. Add a fail-fast check (session.isOpen() && session.isConnected()) at a common entry point such as a request filter

Example fix

// before
session.disconnect();
// ... next request ...
session.createQuery("from User", User.class).list(); // IllegalStateException: No provided connection

// after
session.disconnect();
// ... next request ...
try (Connection c = dataSource.getConnection()) {
  session.reconnect(c);
  return session.createQuery("from User", User.class).list();
}
Defensive patterns

Strategy: validation

Validate before calling

// fail fast before using a user-connection session that may be disconnected
if (!session.isOpen() || !session.isConnected()) {
  throw new IllegalStateException("session must be open and connected before JDBC work");
}

Type guard

static boolean readyForJdbc(org.hibernate.Session s) {
  return s != null && s.isOpen() && s.isConnected();
}

Try / catch

catch (IllegalStateException e) {
  // session is open but holds no connection: reconnect with a fresh connection,
  // or replace with a new session per unit of work
}

Prevention

When it happens

Trigger: Running a query, flush, or beginTransaction() on a user-connection session after session.disconnect() but before session.reconnect(newConnection); lazy-loading triggered on a session cached in disconnected state between requests.

Common situations: Long-conversation/detached-session patterns (disconnect to release the connection, reconnect on next request) where one code path forgets to reconnect; lazy association traversal on a session that was disconnected for serialization.

Related errors


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