hibernate/hibernate-orm · error · JDBCConnectionException

Could not create connection

Error message

Could not create connection

What it means

On dialects that cannot render NULLS FIRST/NULLS LAST (supportsNullPrecedence() == false, e.g. MySQL), the full-join emulation adds a synthesized case-expression select item that encodes null precedence, and records its select index per SortSpecification. This IllegalStateException is an internal invariant failure: rendering found null precedence requested but no recorded index for the sort item, meaning the helper column was never registered - a translator bug, not a user query error per se.

Source

Thrown at hibernate-c3p0/src/main/java/org/hibernate/c3p0/internal/C3P0ConnectionProvider.java:222

					dialect.getVersion(),
					hasSchema,
					hasCatalog,
					schema,
					catalog,
					Boolean.toString( autocommit ),
					isolation == null ? null : toIsolationNiceName( isolation ),
					requireNonNullElse( getInteger( C3P0_STYLE_MIN_POOL_SIZE.substring( 5 ), poolSettings ),
							DEFAULT_MIN_POOL_SIZE ),
					requireNonNullElse( getInteger( C3P0_STYLE_MAX_POOL_SIZE.substring( 5 ), poolSettings ),
							DEFAULT_MAX_POOL_SIZE ),
					fetchSize
			);
			if ( !connection.getAutoCommit() ) {
				connection.rollback();
			}
		}
		catch (SQLException e) {
			throw new JDBCConnectionException( "Could not create connection", e );
		}
	}

	private DataSource createDataSource(String jdbcUrl, Properties connectionProps, Map<String, Object> poolProperties) {
		try {
			return pooledDataSource( unpooledDataSource( jdbcUrl, connectionProps ), poolProperties );
		}
		catch (Exception e) {
			CONNECTION_INFO_LOGGER.unableToInstantiateConnectionPool( e );
			throw new ConnectionProviderConfigurationException(
					"Could not configure c3p0: " + e.getMessage(),  e );
		}
	}

	private void loadDriverClass(String jdbcDriverClass) {
		if ( jdbcDriverClass == null ) {
			CONNECTION_INFO_LOGGER.jdbcDriverNotSpecified();
		}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Remove the explicit nulls first/last for that order item (or unset default_null_precedence) so no emulation column is needed.
  2. Ensure the ordered expression is in the select list - helper-column registration depends on the sort expression indexes being present.
  3. Emulate null precedence manually: order by 'case when x is null then 1 else 0 end, x' as a selected expression.
  4. Upgrade to the newest Hibernate 7.x; if it persists, report an HHH issue with the query - IllegalStateException here indicates a framework defect.

Example fix

// before: explicit null precedence on a dialect without native support
"select a, i.label from A a full join a.items i order by i.label nulls last"

// after: manual null-precedence emulation via a selected case expression
"select a, i.label, case when i.label is null then 1 else 0 end as nl
 from A a full join a.items i order by nl, i.label"
Defensive patterns

Strategy: try-catch

Try / catch

try {
    return query.getResultList();
} catch ( IllegalStateException e ) {
    if ( e.getMessage() != null && e.getMessage().contains("null precedence index is missing") ) {
        // internal translator invariant: drop explicit nulls first/last and retry without full join
        return runWithoutNullPrecedence( query );
    }
    throw e;
}

Prevention

When it happens

Trigger: Full join + ORDER BY with explicit null precedence (HQL 'nulls first/last', Criteria Nulls.FIRST/NULLS.LAST, or the hibernate.default_null_precedence setting) on a dialect without null-precedence support, hitting a code path where collectSortNullPrecedenceEmulationExpressions did not register indexes for that sort specification.

Common situations: Setting default_null_precedence globally and then running full-join queries on MySQL; explicit 'nulls last' on optional sort columns of the full-joined side; regressions after a Hibernate 7.x upgrade on the emulation path.

Related errors


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