How to Fix MySQL Error 3199 (ER_BINLOG_UNSAFE_XA)
You get mysql error 3199 when an app runs an unsafe query inside an XA transaction. To fix it, change your binary log format to row mode or split the transaction. MySQL logs this issue under the symbol ER_BINLOG_UNSAFE_XA and SQLSTATE HY000. The engine warns that the query is unsafe inside an XA block. It also states that concurrent XA transactions may deadlock on a replica. Teams see this alert when apps coordinate work across two or more data stores. What triggers mysql error 3199 in the da
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 3199 when an app runs an unsafe query inside an XA transaction. To fix it, change your binary log format to row mode or split the transaction.
MySQL logs this issue under the symbol ER_BINLOG_UNSAFE_XA and SQLSTATE HY000. The engine warns that the query is unsafe inside an XA block. It also states that concurrent XA transactions may deadlock on a replica. Teams see this alert when apps coordinate work across two or more data stores.
What triggers mysql error 3199 in the database engine
Distributed transactions use the XA protocol to keep multiple databases in sync. A transaction manager coordinates the work across each node in the cluster. It prepares each branch and then commits them all together. This process works well on a single primary server.
Problems start when the primary server sends binary logs to a replica. Statement-based binary logging records the exact SQL text that the app ran. When two XA branches run at the same time, they lock rows in a set order. The primary server handles these locks without an issue.
Replicas read the binary log and replay the statements one by one. If the log uses statement mode, the replica may replay lock requests out of order. Two concurrent XA transactions can then wait on each other forever. This creates a deadlock on the replica and stops data sync.
To prevent this, the database engine flags unsafe queries inside XA blocks. It raises code 3199 to warn the team before a replica stops working. Queries that touch system tables or use non-deterministic functions often trigger this rule. Mixing storage engines inside one XA branch also causes the warning.
How to diagnose and fix the failure in production
First, check your database error logs to find the exact query that failed. Look for the HY000 state and the ER_BINLOG_UNSAFE_XA symbol in your log tool. Trace that query back to the app service that opened the XA branch. Check if the session uses statement-based or mixed binary logging.
The safest fix in production is to switch the binary log to row mode. Row-based logging records the exact row changes instead of the raw query text. This helps the replica apply changes without running into lock order conflicts. You can set this rule globally or for the specific app session.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Next, review the app logic that runs inside the distributed transaction. Keep XA branches short and limit them to simple row updates. Move complex reads, temp table work, and non-transactional writes outside the XA block. Every table in the branch must use the InnoDB engine.
Do not fix this issue by turning off binary logs on the primary node. Skipping the binary log breaks replication and hurts your backup recovery plan. Also, do not grant super admin roles to app accounts to bypass safety checks. Fix the log format and clean up the query structure instead.
How to prevent XA deadlocks on replicas
When replicas apply XA branches in parallel, lock contention can still happen. Check how your replica threads process incoming log events from the primary. Make sure the replica applies transactions in the same commit order as the primary. Preserving commit order stops worker threads from fighting over the same row locks.
Test your distributed transactions in a staging setup with active replicas first. Run load tests that fire multiple XA branches at the same time. Watch the replica status to confirm that no worker threads stall or drop out. Good test habits catch unsafe queries before they reach production data.
Teams should also audit database sessions to see who runs manual XA commands. Developers sometimes test distributed locks by hand during an incident. Put clear rules in place so only approved app services run XA workflows. When humans query production, mask sensitive fields like emails, phone numbers, and card numbers in results.
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 does SQLSTATE HY000 mean with ER_BINLOG_UNSAFE_XA?
SQLSTATE HY000 is a general error class for database engine rules. Paired with code 3199, it means an XA query risks causing a replica deadlock.
Does mysql error 3199 stop the transaction or just warn?
In standard modes, the engine logs a warning and lets the query finish. If strict SQL modes are active, the engine blocks the unsafe statement.
Can row-based logging fix ER_BINLOG_UNSAFE_XA completely?
Row-based logging removes most statement replay risks for standard data changes. You still need to avoid mixing non-transactional tables inside an XA block.
Related guides
In this post