How to Fix MySQL Error 1422 (ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mysql error 1422 when a stored function or trigger tries to run a commit. To fix it, move transaction control or schema changes into a stored procedure or app logic. The database engine returns the message explicit or implicit commit is not allowed in stored function or trigger. It uses the symbol ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG and SQLSTATE HY000. This rule stops functions and triggers from breaking the outer transaction that called them. Why mysql error 1422 happens in the databa

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get mysql error 1422 when a stored function or trigger tries to run a commit. To fix it, move transaction control or schema changes into a stored procedure or app logic.

The database engine returns the message explicit or implicit commit is not allowed in stored function or trigger. It uses the symbol ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG and SQLSTATE HY000. This rule stops functions and triggers from breaking the outer transaction that called them.

Why mysql error 1422 happens in the database engine

A stored function or trigger always runs inside the context of a parent SQL statement. That parent statement is already part of an active transaction in the database engine. If the trigger or function ends the transaction early, the parent statement loses its atomic state.

Two kinds of actions cause this error in your database. The first cause is an explicit transaction statement written inside the routine body. This happens when a developer adds a commit or rollback command directly into the routine.

The second cause is an implicit commit triggered by data definition language commands. Commands that create, alter, drop, or truncate tables force the engine to commit right away. User management commands and table lock changes also force an immediate commit in the engine. Even calling a stored procedure that contains these commands from a trigger will fail.

How to find and fix the failure in production

Start by reading the full text of the trigger or stored function that failed. Look for direct commit or rollback statements in the body and remove them. Let the calling app or outer stored procedure manage the start and end of transactions.

If you do not see direct transaction commands, check for schema change statements. A trigger cannot drop or truncate a summary table during a row update. Replace the truncate command with a standard delete statement to keep the current transaction intact. If the routine creates a table on the fly, create a temporary table before the query runs.

Just-in-Time Access + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Sometimes the trigger or function calls a separate stored procedure to do extra work. You must inspect every nested procedure in that call chain for hidden commits. Move any table changes or transaction borders out to the main stored procedure or app layer.

How to prevent transaction conflicts during schema updates

Teams often hit this error when they build audit logs or data cleanup rules inside triggers. A developer might try to manage partitions or clear staging tables when a new row arrives. Keep triggers small and limit them to simple row inserts, updates, or deletes.

When you need to run schema updates alongside data changes, use a stored procedure instead. Stored procedures run as top-level calls and can commit work whenever needed. Good review habits during database updates help your team catch forbidden statements before code reaches production.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.

With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

Can I use a rollback statement inside a MySQL trigger to cancel bad data?

No, a rollback statement inside a trigger will cause error 1422 right away. Instead, raise an error signal in the trigger so the engine aborts the outer statement.

Why does my trigger fail with error 1422 when it only calls a stored procedure?

The rule applies to the entire call chain started by the trigger or function. If the called stored procedure runs an explicit or implicit commit, the trigger still fails.

Does truncating a table inside a stored function cause this error?

Yes, truncating a table acts as a schema change and forces an implicit commit. Use a standard delete query inside the stored function to remove rows without committing.

The query runs. The emails stay hidden.