hibernate/hibernate-orm · error · IllegalStateException

Cannot serialize {} [{}] as it has a shared TransactionCoord

Error message

Cannot serialize {} [{}] as it has a shared TransactionCoordinator

What it means

Error "Cannot serialize {} [{}] as it has a shared TransactionCoordinator" thrown in hibernate/hibernate-orm.

Source

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

	@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

		factory.serialize( oos );
		oos.writeObject( jdbcSessionContext.getStatementObserver() );
		oos.writeObject( jdbcSessionContext.getStatementInspector() );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Do not serialize sessions built with a shared TransactionCoordinator; create an independent session for serialization.

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/4713eefc7bf1d186. Report an issue: GitHub.