How to Fix MySQL Error 1790 (ER_CANT_SET_GTID_NEXT_WHEN_OWNING_GTID)
You trigger mysql error 1790 when a database session tries to change its transaction ID while holding an active one. You fix it by committing or rolling back the open transaction before assigning a new ID. MySQL uses Global Transaction Identifiers, or GTIDs, to track changes across replicas. Each session can hold only one transaction ID at a time. When a script or admin tries to set a second ID before finishing the first work unit, the engine stops the query. This rule keeps replication logs cl
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 trigger mysql error 1790 when a database session tries to change its transaction ID while holding an active one. You fix it by committing or rolling back the open transaction before assigning a new ID.
MySQL uses Global Transaction Identifiers, or GTIDs, to track changes across replicas. Each session can hold only one transaction ID at a time. When a script or admin tries to set a second ID before finishing the first work unit, the engine stops the query. This rule keeps replication logs clean and prevents data loss across clusters.
What causes mysql error 1790 in your database
The database server tracks this problem under the symbol ER_CANT_SET_GTID_NEXT_WHEN_OWNING_GTID and SQLSTATE HY000. The engine returns a clear message stating that the session variable cannot change while the client owns a transaction ID. It also prints the exact ID that the client holds right now. Finally, the message notes that ownership ends on commit.
This failure often happens during manual replication fixes. Database admins sometimes inject empty transactions to skip broken replication events. If the admin sets the session transaction ID and forgets to finish the transaction, the session keeps ownership. Trying to set another transaction ID in that same session triggers the error right away.
Backup restores can also cause this issue. When you run a dump file on a target database, the script sets transaction IDs for each statement group. If a statement fails mid-flight or a transaction keeps the session active, the next ID assignment will fail. Connection pools in apps can cause similar problems if a pooled session holds an uncommitted state.
How to diagnose and fix the failure in production
First, look at the error message text from the server. It tells you the exact transaction ID that your current session owns. Next, check the active session state to see if any uncommitted data modifications exist. You should never grant broad admin rights to app roles just to bypass replication checks.
If you are trying to skip a bad replication event with an empty transaction, the fix is simple. You must run an explicit commit or rollback statement in your current session. Commit finishes the empty transaction and writes the ID to the binary log. Rollback cancels the work and drops ownership without logging the transaction ID.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Once ownership ends, your session can claim a new transaction ID without errors. If this happens during a backup import, check why the prior transaction did not close. Often, an earlier query hit a lock timeout or syntax error and left the block open. Fix that earlier query failure first, then run your restore script in a fresh session.
How to prevent transaction ID conflicts during database maintenance
Human error during manual repair work causes most transaction ownership bugs. When engineers type replication commands by hand, it is easy to miss a commit step. Standardizing how your team runs production maintenance reduces these mistakes. You should pair every transaction ID assignment with an immediate commit or rollback in one tested block.
You should also audit every query that modifies replication state in production. Limit who can set session variables on primary and replica nodes. Using a central proxy layer helps enforce these rules across all database sessions. Meanwhile, data masking rewrites matched fields like emails and card numbers in query results during debugging.
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
What releases GTID ownership in a MySQL session?
Running a commit or rollback statement releases ownership of the current transaction ID. Closing the client session also drops the uncommitted transaction and frees the ID.
Why does mysql error 1790 happen when skipping a replication error?
Admins skip errors by assigning the failing transaction ID and committing an empty block. The error happens if you assign a second ID before committing the first one.
Does rolling back a transaction write the GTID to the binary log?
No, a rollback cancels the transaction and releases ownership without logging the ID. You must commit an empty transaction if you want the replica to skip that ID.
Related guides
In this post