Skip to content

Commit dc8da19

Browse files
committed
docs: add reset connection and resetOnRelease documentation
Add new documentation page covering connection.reset() with promise/callback examples, the resetOnRelease pool option, error handling, and MySQL version compatibility. Link from the documentation index and update the prepared-statements page to reference reset().
1 parent e63aa51 commit dc8da19

3 files changed

Lines changed: 196 additions & 1 deletion

File tree

website/docs/documentation/00-index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Not only **MySQL2** offers better performance over [Node MySQL][node-mysql], we
2121

2222
- [Prepared Statements](/docs/documentation/prepared-statements)
2323
- [Query Attributes](/docs/documentation/query-attributes)
24+
- [Reset Connection](/docs/documentation/reset-connection)
2425
- [Promise Wrapper](/docs/documentation/promise-wrapper)
2526
- [Authentication Switch](/docs/documentation/authentication-switch)
2627
- [More Features](/docs/documentation/extras)

website/docs/documentation/prepared-statements.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ connection.prepare('select ? + ? as tests', (err, statement) => {
4141
});
4242
```
4343

44-
Note that you should not use statement after connection reset (`changeUser()` or disconnect). Statement scope is connection, you need to prepare statement for each new connection in order to use it.
44+
Note that you should not use statement after connection reset (`changeUser()`, [`reset()`](/docs/documentation/reset-connection) or disconnect). Statement scope is connection, you need to prepare statement for each new connection in order to use it.
4545

4646
## Configuration
4747

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
---
2+
tags: [reset, resetOnRelease, pool, COM_RESET_CONNECTION]
3+
---
4+
5+
import Tabs from '@theme/Tabs';
6+
import TabItem from '@theme/TabItem';
7+
8+
# Reset Connection
9+
10+
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.
11+
12+
## What gets reset
13+
14+
| State | Cleared? | Details |
15+
| ------------------- | -------- | --------------------------------------- |
16+
| User variables | Yes | All `@variable` values cleared |
17+
| Temporary tables | Yes | All temp tables dropped |
18+
| Prepared statements | Yes | Server invalidates, client clears cache |
19+
| Session variables | Yes | Reset to global defaults |
20+
| Locks (GET_LOCK) | Yes | All named locks released |
21+
| Active transaction | Yes | Rolled back if active |
22+
| User / Database | No | Unchanged (use `changeUser` for this) |
23+
24+
<hr />
25+
26+
## connection.reset()
27+
28+
> **reset(callback?: (err: Error | null) => void): void**
29+
30+
Resets the connection session state. The connection remains open and authenticated.
31+
32+
<Tabs>
33+
<TabItem value='promise.js' default>
34+
35+
```js
36+
import mysql from 'mysql2/promise';
37+
38+
const connection = await mysql.createConnection({
39+
host: 'localhost',
40+
user: 'root',
41+
database: 'test',
42+
});
43+
44+
await connection.query('SET @user_id = 123');
45+
46+
// highlight-next-line
47+
await connection.reset();
48+
49+
// @user_id is now NULL
50+
const [rows] = await connection.query('SELECT @user_id as val');
51+
console.log(rows[0].val); // null
52+
53+
await connection.end();
54+
```
55+
56+
</TabItem>
57+
<TabItem value='callback.js'>
58+
59+
```js
60+
const mysql = require('mysql2');
61+
62+
const connection = mysql.createConnection({
63+
host: 'localhost',
64+
user: 'root',
65+
database: 'test',
66+
});
67+
68+
connection.query('SET @user_id = 123', (err) => {
69+
if (err) throw err;
70+
71+
// highlight-next-line
72+
connection.reset((err) => {
73+
if (err) throw err;
74+
75+
// @user_id is now NULL
76+
connection.query('SELECT @user_id as val', (err, rows) => {
77+
if (err) throw err;
78+
console.log(rows[0].val); // null
79+
connection.end();
80+
});
81+
});
82+
});
83+
```
84+
85+
</TabItem>
86+
</Tabs>
87+
88+
:::tip
89+
`connection.reset()` is ~3-5x faster than `changeUser()` for clearing session state because it does not require re-authentication.
90+
:::
91+
92+
:::caution
93+
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.
94+
:::
95+
96+
<hr />
97+
98+
## Pool: resetOnRelease
99+
100+
> **resetOnRelease?: boolean** (Default: `false`)
101+
102+
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.
103+
104+
<Tabs>
105+
<TabItem value='promise.js' default>
106+
107+
```js
108+
import mysql from 'mysql2/promise';
109+
110+
// highlight-start
111+
const pool = mysql.createPool({
112+
host: 'localhost',
113+
user: 'root',
114+
database: 'test',
115+
resetOnRelease: true,
116+
});
117+
// highlight-end
118+
119+
const conn1 = await pool.getConnection();
120+
await conn1.query("SET @secret = 'sensitive_data'");
121+
conn1.release(); // triggers automatic reset
122+
123+
const conn2 = await pool.getConnection();
124+
const [rows] = await conn2.query('SELECT @secret as val');
125+
console.log(rows[0].val); // null — state was cleared
126+
conn2.release();
127+
128+
await pool.end();
129+
```
130+
131+
</TabItem>
132+
<TabItem value='callback.js'>
133+
134+
```js
135+
const mysql = require('mysql2');
136+
137+
// highlight-start
138+
const pool = mysql.createPool({
139+
host: 'localhost',
140+
user: 'root',
141+
database: 'test',
142+
resetOnRelease: true,
143+
});
144+
// highlight-end
145+
146+
pool.getConnection((err, conn1) => {
147+
if (err) throw err;
148+
149+
conn1.query("SET @secret = 'sensitive_data'", (err) => {
150+
if (err) throw err;
151+
conn1.release(); // triggers automatic reset
152+
153+
pool.getConnection((err, conn2) => {
154+
if (err) throw err;
155+
156+
conn2.query('SELECT @secret as val', (err, rows) => {
157+
if (err) throw err;
158+
console.log(rows[0].val); // null — state was cleared
159+
conn2.release();
160+
pool.end();
161+
});
162+
});
163+
});
164+
});
165+
```
166+
167+
</TabItem>
168+
</Tabs>
169+
170+
### Error handling
171+
172+
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.
173+
174+
### Why resetOnRelease defaults to false
175+
176+
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`.
177+
178+
We recommend explicitly enabling it in new projects:
179+
180+
```js
181+
const pool = mysql.createPool({
182+
// ...
183+
resetOnRelease: true,
184+
});
185+
```
186+
187+
### MySQL version compatibility
188+
189+
| Version | Support |
190+
| ------------------- | ------------- |
191+
| MySQL 5.7.3+ | Full support |
192+
| MySQL 5.7.0–5.7.2 | Not available |
193+
| MySQL 5.6 and older | Not available |
194+
| MariaDB 10.2.4+ | Full support |

0 commit comments

Comments
 (0)