hibernate/hibernate-orm · error · PersistenceException

Could not set provided connection [%s] to auto-commit mode (

Error message

Could not set provided connection [%s] to auto-commit mode (needed for schema generation)

What it means

When schema generation runs against a user-supplied Connection (JdbcConnectionAccessProvidedConnectionImpl), Hibernate temporarily forces auto-commit=true because DDL must execute in auto-commit mode, and restores the original state afterwards. This exception means jdbcConnection.setAutoCommit(true) threw a SQLException: the driver or datasource rejected the switch. The original SQLException is chained, so the cause (active transaction, read-only, XA-managed) is visible via getCause().

Source

Thrown at hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/JdbcConnectionAccessProvidedConnectionImpl.java:40

	private final Connection jdbcConnection;
	private final boolean wasInitiallyAutoCommit;

	public JdbcConnectionAccessProvidedConnectionImpl(Connection jdbcConnection) {
		this.jdbcConnection = jdbcConnection;
		wasInitiallyAutoCommit = enableAutoCommit( jdbcConnection );
		JDBC_LOGGER.initialAutoCommit( wasInitiallyAutoCommit );
	}

	private static boolean enableAutoCommit(Connection jdbcConnection) {
		try {
			final boolean wasInitiallyAutoCommit = jdbcConnection.getAutoCommit();
			if ( !wasInitiallyAutoCommit ) {
				try {
					jdbcConnection.setAutoCommit( true );
				}
				catch (SQLException exception) {
					throw new PersistenceException(
							String.format(
									"Could not set provided connection [%s] to auto-commit mode" +
											" (needed for schema generation)",
									jdbcConnection
							),
							exception
					);
				}
			}
			return wasInitiallyAutoCommit;
		}
		catch (SQLException ignore) {
			return false;
		}
	}

	@Override
	public Connection obtainConnection() throws SQLException {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Commit or roll back any open transaction on the connection before handing it to schema generation.
  2. Set autoCommit=true yourself before passing the connection, so Hibernate's set call is a no-op.
  3. Ensure the connection is writable (not read-only) and not enrolled in a JTA/XA transaction.
  4. Prefer letting Hibernate obtain connections through a ConnectionProvider (TargetType.DATABASE with configured datasource) instead of a supplied connection.

Example fix

// before: raw connection possibly mid-transaction
schemaExport.execute( EnumSet.of( TargetType.DATABASE ), connection );

// after: hand over a clean auto-commit, writable connection
if ( !connection.getAutoCommit() ) {
    connection.commit(); // end any open transaction
    connection.setAutoCommit( true );
}
schemaExport.execute( EnumSet.of( TargetType.DATABASE ), connection );
Defensive patterns

Strategy: validation

Validate before calling

// run before schema generation with a supplied connection
if ( connection.isReadOnly() ) {
    throw new IllegalStateException( "Connection must be writable for schema generation" );
}
if ( !connection.getAutoCommit() ) {
    if ( !connection.getAutoCommit() && connection.getTransactionState != null ) { /* driver-specific txn check */ }
    connection.commit();            // end any open transaction first
    connection.setAutoCommit( true );
}

Try / catch

try {
    schemaExport.execute( EnumSet.of( TargetType.DATABASE ), connection );
}
catch ( PersistenceException e ) {
    if ( e.getCause() instanceof SQLException sql ) {
        // inspect sql for active-transaction / read-only / managed-connection errors
    }
    throw e;
}

Prevention

When it happens

Trigger: Passing your own Connection to schema tools — SchemaExport.execute(..., connection, ...), SchemaUpdate/SchemaValidator with a supplied connection, or jakarta.persistence.schema-generation.database.connection — while the connection is inside an open transaction, is read-only, or belongs to a managed/XA pool that forbids auto-commit changes.

Common situations: App-server managed datasource used for hbm2ddl; connection with autoCommit=false and an active transaction; read-only replica connections; JTA/XA-enrolled connections; some cloud pools (e.g. connection-in-use state) rejecting setAutoCommit.

Related errors


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