{"record":{"id":"4ea027a70f3a3711","repo":"hibernate/hibernate-orm","slug":"could-not-set-provided-connection-s-to-auto-com-4ea027","errorCode":null,"errorMessage":"Could not set provided connection [%s] to auto-commit mode (needed for schema generation)","messagePattern":"Could not set provided connection \\[(.+?)\\] to auto-commit mode \\(needed for schema generation\\)","errorType":"exception","errorClass":"PersistenceException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/JdbcConnectionAccessProvidedConnectionImpl.java","lineNumber":40,"sourceCode":"\n\tprivate final Connection jdbcConnection;\n\tprivate final boolean wasInitiallyAutoCommit;\n\n\tpublic JdbcConnectionAccessProvidedConnectionImpl(Connection jdbcConnection) {\n\t\tthis.jdbcConnection = jdbcConnection;\n\t\twasInitiallyAutoCommit = enableAutoCommit( jdbcConnection );\n\t\tJDBC_LOGGER.initialAutoCommit( wasInitiallyAutoCommit );\n\t}\n\n\tprivate static boolean enableAutoCommit(Connection jdbcConnection) {\n\t\ttry {\n\t\t\tfinal boolean wasInitiallyAutoCommit = jdbcConnection.getAutoCommit();\n\t\t\tif ( !wasInitiallyAutoCommit ) {\n\t\t\t\ttry {\n\t\t\t\t\tjdbcConnection.setAutoCommit( true );\n\t\t\t\t}\n\t\t\t\tcatch (SQLException exception) {\n\t\t\t\t\tthrow new PersistenceException(\n\t\t\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\t\t\"Could not set provided connection [%s] to auto-commit mode\" +\n\t\t\t\t\t\t\t\t\t\t\t\" (needed for schema generation)\",\n\t\t\t\t\t\t\t\t\tjdbcConnection\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\texception\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn wasInitiallyAutoCommit;\n\t\t}\n\t\tcatch (SQLException ignore) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t@Override\n\tpublic Connection obtainConnection() throws SQLException {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/JdbcConnectionAccessProvidedConnectionImpl.java#L22-L58","documentation":"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().","triggerScenarios":"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.","commonSituations":"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.","solutions":["Commit or roll back any open transaction on the connection before handing it to schema generation.","Set autoCommit=true yourself before passing the connection, so Hibernate's set call is a no-op.","Ensure the connection is writable (not read-only) and not enrolled in a JTA/XA transaction.","Prefer letting Hibernate obtain connections through a ConnectionProvider (TargetType.DATABASE with configured datasource) instead of a supplied connection."],"exampleFix":"// before: raw connection possibly mid-transaction\nschemaExport.execute( EnumSet.of( TargetType.DATABASE ), connection );\n\n// after: hand over a clean auto-commit, writable connection\nif ( !connection.getAutoCommit() ) {\n    connection.commit(); // end any open transaction\n    connection.setAutoCommit( true );\n}\nschemaExport.execute( EnumSet.of( TargetType.DATABASE ), connection );","handlingStrategy":"validation","validationCode":"// run before schema generation with a supplied connection\nif ( connection.isReadOnly() ) {\n    throw new IllegalStateException( \"Connection must be writable for schema generation\" );\n}\nif ( !connection.getAutoCommit() ) {\n    if ( !connection.getAutoCommit() && connection.getTransactionState != null ) { /* driver-specific txn check */ }\n    connection.commit();            // end any open transaction first\n    connection.setAutoCommit( true );\n}","typeGuard":null,"tryCatchPattern":"try {\n    schemaExport.execute( EnumSet.of( TargetType.DATABASE ), connection );\n}\ncatch ( PersistenceException e ) {\n    if ( e.getCause() instanceof SQLException sql ) {\n        // inspect sql for active-transaction / read-only / managed-connection errors\n    }\n    throw e;\n}","preventionTips":["Always hand schema tooling an auto-commit, writable, unmanaged connection.","Do not run hbm2ddl with a connection enrolled in JTA/XA.","If you must pass a pooled connection, reset auto-commit yourself first so Hibernate's call is a no-op."],"tags":["hibernate","jdbc","schema-generation","autocommit","persistence-exception"],"backgroundTag":"set-autocommit-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}