hibernate/hibernate-orm · error · IllegalArgumentException

Cannot reconnect to a new user-supplied connection because c

Error message

Cannot reconnect to a new user-supplied connection because currently connected; must disconnect before reconnecting.

What it means

IllegalArgumentException from LogicalConnectionProvidedImpl.manualReconnect(): reconnect was called with a DIFFERENT Connection while the session still holds its original user-supplied connection (providedConnection != null). A session swaps connections only through the disconnect/reconnect pair; reconnecting the very same connection is tolerated (just logged), but a new one while connected is rejected.

Source

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

			return providedConnection;
		}
		finally {
			providedConnection = null;
		}
	}

	@Override
	public void manualReconnect(@Nonnull Connection connection) {
		errorIfClosed();
		if ( connection == null ) {
			throw new IllegalArgumentException( "cannot reconnect using a null connection" );
		}
		else if ( connection == providedConnection ) {
			// likely an unmatched reconnect call (no matching disconnect call)
			CONNECTION_LOGGER.reconnectingSameConnectionAlreadyConnected();
		}
		else if ( providedConnection != null ) {
			throw new IllegalArgumentException(
					"Cannot reconnect to a new user-supplied connection because currently connected; must disconnect before reconnecting."
			);
		}
		providedConnection = connection;
		CONNECTION_LOGGER.manuallyReconnectedLogicalConnection();
	}

	@Override
	protected Connection getConnectionForTransactionManagement() {
		return providedConnection;
	}

	@Override
	protected void afterCompletion() {
		afterTransaction();
		resetConnection( initiallyAutoCommit );
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Always pair reconnect with a prior disconnect: if (session.isConnected()) session.disconnect();
  2. Only reconnect when the session is actually disconnected — guard the call
  3. For multi-tenant routing use Hibernate multi-tenancy or distinct sessions per tenant, not connection swapping

Example fix

// before
session.reconnect(requestConnection); // throws: still holding previous connection

// after
if (session.isConnected()) session.disconnect();
session.reconnect(requestConnection);
Defensive patterns

Strategy: validation

Validate before calling

// swap connections only through the disconnect -> reconnect pair
if (session.isConnected()) {
  session.disconnect();
}
session.reconnect(requestConnection);

Type guard

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

Try / catch

catch (IllegalArgumentException e) {
  // reconnect was rejected because the session still holds a connection:
  // disconnect() first, then retry reconnect with the new connection
}

Prevention

When it happens

Trigger: session.reconnect(newConnection) without a prior session.disconnect(); calling reconnect twice with different connections per request; swapping connections for routing/tenancy by reconnect while connected.

Common situations: Per-request code that reconnects unconditionally; error paths that skip disconnect; legacy multi-tenant hacks that try to point a live session at another database.

Related errors


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