Common SQL Errors

MySQL Error 1685 ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_DIRECT: How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>Raised when a session tries to change @@session.binlog_direct_non_transactional_updates while a transaction is active.</p>

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1685 ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_DIRECT?

<p>MySQL Error 1685: ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_DIRECT occurs when you set @@session.binlog_direct_non_transactional_updates inside an open transaction. Commit or roll back first, then rerun the SET, or configure the variable globally before any transaction to resolve the issue.</p>

Error Highlights

Typical Error Message

Cannot modify

Error Type

Transaction Error

Language

MySQL

Symbol

ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_DIRECT

Error Code

1685

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 1685 ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_DIRECT?

MySQL throws error 1685 when a client attempts to modify the session variable @@session.binlog_direct_non_transactional_updates while a transaction is already in progress. The server blocks the change to maintain binary log consistency.

The condition name ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_DIRECT makes it clear that an active transaction prevents switching the binlog_direct_non_transactional_updates flag. The error appears immediately after the SET statement.

What Causes This Error?

The error is triggered by executing SET @@session.binlog_direct_non_transactional_updates = 1 or 0 within a BEGIN ... COMMIT or START TRANSACTION block. MySQL mandates that the variable be set before any transactional statement.

Another common cause is client frameworks that disable autocommit, creating implicit transactions. A mid-session SET then fails because the session is never truly outside a transaction.

How to Fix MySQL Error 1685

Commit or roll back the current transaction, then reissue the SET command. This simple sequence clears the transactional context and allows the variable change.

You can also define the variable globally or at connection startup so it never needs modification during a transaction. This strategy works well with connection pools and batch jobs.

Common Scenarios and Solutions

Migration scripts that bundle variable changes with DML often hit this error. Split the script so the SET command runs alone, followed by a new transaction for data updates.

In connection pools with autocommit OFF, place SET @@session.binlog_direct_non_transactional_updates = 1 in the initialization SQL so every new session starts with the correct value.

Best Practices to Avoid This Error

Always set session variables affecting binary logging before beginning any transaction. Make it part of your application startup routine or session initialization.

Add error 1685 monitoring to DBA dashboards. Quick detection pinpoints clients that attempt mid-transaction variable switches.

Related Errors and Solutions

Error 1663 ER_CANT_CHANGE_TX_CHARACTERISTICS_DRIVED blocks changing isolation levels inside a transaction. Commit, then change the level.

Error 1224 ER_WARNING_NOT_COMPLETE_ROLLBACK signals an incomplete rollback when autocommit is OFF. Review transaction boundaries and retry.

Common Causes

SET executed inside BEGIN block

Issuing SET @@session.binlog_direct_non_transactional_updates inside an explicit transaction triggers error 1685.

Implicit transactions via autocommit OFF

With autocommit disabled, every statement runs inside a transaction, making mid-session SET commands illegal.

Framework scripts that bundle SET and DML

Migrations or ORM batches often mix variable toggles and data changes, unintentionally causing the error.

Related Errors

MySQL Error 1663 ER_CANT_CHANGE_TX_CHARACTERISTICS_DRIVED

Raised when changing isolation level inside a transaction. Commit first, then alter the level.

MySQL Error 1192 ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION

Occurs when attempting writes in read-only mode. Switch to read-write or end the read-only transaction.

MySQL Error 1224 ER_WARNING_NOT_COMPLETE_ROLLBACK

Indicates a rollback could not undo all changes. Inspect binary log for partial commits.

FAQs

Can I disable the restriction entirely?

No. MySQL enforces it to protect binary log integrity. You can only change the variable outside a transaction.

Does autocommit ON avoid the error?

Yes. With autocommit ON, each statement is its own transaction, leaving no open transaction when you run the SET.

Is the error version-specific?

The restriction exists in MySQL 5.7, 8.0, and later releases with identical behavior.

How does Galaxy help?

Galaxy highlights open transactions, lets you run SET statements separately, and provides snippets that commit first, reducing the chance of error 1685.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo