hibernate/hibernate-orm · error · IllegalStateException

Cannot serialize {} [{}] while connected

Error message

Cannot serialize {} [{}] while connected

What it means

Error "Cannot serialize {} [{}] while connected" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java:2588

			sessionAssociationMarkers = new SessionAssociationMarkers( this );
		}
		return sessionAssociationMarkers;
	}

	@Override
	public <E extends Extension> E getExtension(Class<E> extension) {
		return extension.cast( extensions.get( extension ) );
	}

	@Serial
	private void writeObject(ObjectOutputStream oos) throws IOException {
		SESSION_LOGGER.serializingSession( getSessionIdentifier() );


		if ( !jdbcCoordinator.isReadyForSerialization() ) {
			// throw a more specific (helpful) exception message when this happens from Session,
			//		as opposed to more generic exception from jdbcCoordinator#serialize call later
			throw new IllegalStateException( "Cannot serialize " + getClass().getSimpleName() + " [" + getSessionIdentifier() + "] while connected" );
		}

		if ( isTransactionCoordinatorShared ) {
			throw new IllegalStateException( "Cannot serialize " + getClass().getSimpleName() + " [" + getSessionIdentifier() + "] as it has a shared TransactionCoordinator" );
		}

		// todo : (5.2) come back and review serialization plan...
		//		this was done quickly during initial HEM consolidation into CORE and is likely not perfect :)
		//
		//		be sure to review state fields in terms of transient modifiers

		// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		// Step 1 :: write non-transient state...
		oos.defaultWriteObject();

		// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		// Step 2 :: write transient state...
		// 		-- see concurrent access discussion

View on GitHub (pinned to fad1729dce)

Solutions

  1. Disconnect (close) the session before serializing it, or avoid serializing connected sessions.

When it happens

Trigger: Thrown when a Session/EntityManager that cannot be safely serialized is serialized.

Common situations: Typical situations: serializing a session while it holds a JDBC connection or a shared TransactionCoordinator; putting a live session in an HTTP session.


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