hibernate/hibernate-orm · error · IllegalStateException
Cannot manually reconnect unless Connection was originally s
Error message
Cannot manually reconnect unless Connection was originally supplied by user
What it means
IllegalStateException from LogicalConnectionManagedImpl.manualReconnect(): manual reconnection with a user-supplied Connection is only supported when the session was originally opened WITH a user-supplied connection (LogicalConnectionProvidedImpl). This session uses a Hibernate/pool-managed connection, so manual reconnect is structurally unsupported and always throws.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionManagedImpl.java:193
}
}
@Override
public Connection manualDisconnect() {
if ( closed ) {
throw new ResourceClosedException( "Logical connection is closed" );
}
final var connection = physicalConnection;
releaseConnectionIfNeeded();
return connection;
}
@Override
public void manualReconnect(@Nonnull Connection suppliedConnection) {
if ( closed ) {
throw new ResourceClosedException( "Logical connection is closed" );
}
throw new IllegalStateException( "Cannot manually reconnect unless Connection was originally supplied by user" );
}
private Connection acquire() {
final var eventHandler = getJdbcSessionContext().getEventHandler();
eventHandler.jdbcConnectionAcquisitionStart();
try {
return getJdbcConnectionAccess().obtainConnection();
}
catch ( SQLException e ) {
throw getExceptionHelper().convert( e, "Unable to acquire JDBC Connection" );
}
finally {
eventHandler.jdbcConnectionAcquisitionEnd( physicalConnection );
}
}
private void release(Connection connection) {
final var eventHandler = getJdbcSessionContext().getEventHandler();View on GitHub (pinned to fad1729dce)
Solutions
- Remove the reconnect call and rely on Hibernate connection handling modes (hibernate.connection.handling_mode) for acquisition/release
- If you truly need manual connection control, open the session with your own connection: sessionFactory.openSession(myConnection)
- For per-tenant/dynamic connections use Hibernate multi-tenancy or a routing ConnectionProvider, not manual reconnect
Example fix
// before
Session s = sessionFactory.openSession(); // pooled/managed connection
...
s.reconnect(myConnection); // IllegalStateException
// after
// choose the pattern once, at open time:
Session s = manualControlNeeded
? sessionFactory.openSession(myConnection) // user-supplied: disconnect/reconnect allowed
: sessionFactory.openSession(); // pooled: let Hibernate manage the connection Defensive patterns
Strategy: validation
Validate before calling
// decide the pattern at open time; never call reconnect() on pooled sessions
try (Session s = manualConnectionControlNeeded
? sessionFactory.openSession(userConnection)
: sessionFactory.openSession()) {
// ...
} Try / catch
catch (IllegalStateException e) {
// manual reconnect is unsupported for pool-managed sessions: restructure to open a
// new session (optionally with a user-supplied connection) instead of reconnecting
} Prevention
- Only sessions opened via openSession(connection) support manual disconnect/reconnect
- Default to letting hibernate.connection.handling_mode manage connections
- Use Hibernate multi-tenancy or separate sessions per tenant instead of swapping connections
When it happens
Trigger: Calling session.reconnect(connection) on any session opened normally — sessionFactory.openSession(), Spring/CDI-managed sessions, @PersistenceContext-injected managers — i.e. anything not opened via sessionFactory.openSession(userConnection).
Common situations: Disconnect/reconnect long-conversation code ported from very old Hibernate documentation applied to modern pooled sessions; attempts to hot-swap the JDBC connection under a live session (routing, testing) using reconnect.
Related errors
- Cannot reconnect to a new user-supplied connection because c
- immutable global instance of LockOptions
- Cannot lazily initialize collection
- Illegal attempt to associate a collection with two open sess
- Cannot serialize Session while connected
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/3d12f6f308dcbbe6.
Report an issue: GitHub.