跳到主要内容

Reset Connection

MySQL's COM_RESET_CONNECTION command (available since MySQL 5.7.3 and MariaDB 10.2.4) resets a connection's session state without closing the underlying TCP connection or re-authenticating. It is significantly faster than changeUser() for clearing session state.

What gets reset

StateCleared?Details
User variablesYesAll @variable values cleared
Temporary tablesYesAll temp tables dropped
Prepared statementsYesServer invalidates, client clears cache
Session variablesYesReset to global defaults
Locks (GET_LOCK)YesAll named locks released
Active transactionYesRolled back if active
User / DatabaseNoUnchanged (use changeUser for this)

connection.reset()

reset(callback?: (err: Error | null) => void): void

Resets the connection session state. The connection remains open and authenticated.

import mysql from 'mysql2/promise';

const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test',
});

await connection.query('SET @user_id = 123');

await connection.reset();

// @user_id is now NULL
const [rows] = await connection.query('SELECT @user_id as val');
console.log(rows[0].val); // null

await connection.end();
提示

connection.reset() is ~3-5x faster than changeUser() for clearing session state because it does not require re-authentication.

警告

Prepared statements are invalidated on the server after a reset. The client cache is cleared automatically, but any PreparedStatementInfo references you hold become invalid. Re-execute statements after resetting.


Pool: resetOnRelease

resetOnRelease?: boolean (Default: false)

When set to true on a pool, every connection is automatically reset via COM_RESET_CONNECTION when it is released back to the pool. This ensures the next consumer receives a clean connection with no leftover session state.

import mysql from 'mysql2/promise';

const pool = mysql.createPool({
host: 'localhost',
user: 'root',
database: 'test',
resetOnRelease: true,
});

const conn1 = await pool.getConnection();
await conn1.query("SET @secret = 'sensitive_data'");
conn1.release(); // triggers automatic reset

const conn2 = await pool.getConnection();
const [rows] = await conn2.query('SELECT @secret as val');
console.log(rows[0].val); // null — state was cleared
conn2.release();

await pool.end();

Error handling

If a reset fails (e.g., the MySQL server version does not support COM_RESET_CONNECTION), the pool automatically destroys the faulty connection and creates a fresh one for the next request. No manual intervention is required.

Why resetOnRelease defaults to false

While resetOnRelease: true is the safer behavior for production applications, it defaults to false to avoid breaking existing applications that may rely on session state persisting across pool connection reuse. In a future major version, the default is expected to change to true.

We recommend explicitly enabling it in new projects:

const pool = mysql.createPool({
// ...
resetOnRelease: true,
});

MySQL version compatibility

VersionSupport
MySQL 5.7.3+Full support
MySQL 5.7.0–5.7.2Not available
MySQL 5.6 and olderNot available
MariaDB 10.2.4+Full support