Skip to content

Comments

feat: add Symbol.dispose and Symbol.asyncDispose support for Connections, Pools, and Pool Clusters#4112

Merged
wellwelwel merged 7 commits intosidorares:masterfrom
wellwelwel:dispose
Feb 23, 2026
Merged

feat: add Symbol.dispose and Symbol.asyncDispose support for Connections, Pools, and Pool Clusters#4112
wellwelwel merged 7 commits intosidorares:masterfrom
wellwelwel:dispose

Conversation

@wellwelwel
Copy link
Collaborator

@wellwelwel wellwelwel commented Feb 23, 2026

This PR adds Symbol.dispose and Symbol.asyncDispose support for automatic resource cleanup via Explicit Resource Management.

Closes #3590 (alternative PR).
Indirectly, closes #2725, closes #2543, and closes #2751.

Usage examples

Tip

.end() and .release() are called automatically when leaving their scope for both callback and promise createConnection, createPool, getConnection, and createPoolCluster APIs.

mysql2/promise: await using

import mysql from 'mysql2/promise';

// Connection
{
  await using conn = await mysql.createConnection({});

  const [rows] = await conn.query('SELECT * FROM users');
  console.log(rows);
}

// Pool
{
  await using pool = mysql.createPool({});
  await using conn = await pool.getConnection();

  const [rows] = await conn.query('SELECT * FROM users');
  console.log(rows);
}

// PoolCluster
{
  await using cluster = mysql.createPoolCluster();

  cluster.add('MASTER', {});

  const [rows] = await cluster.of('*').query('SELECT * FROM users');
  console.log(rows);
}

mysql2 (callback): using

import mysql from 'mysql2';

// Connection
{
  using conn = mysql.createConnection({});

  conn.query('SELECT * FROM users', (err, rows) => {
    console.log(rows);
  });
}

// Pool
{
  using pool = mysql.createPool({});

  pool.getConnection((err, _conn) => {
    using conn = _conn;

    conn.query('SELECT * FROM users', (err, rows) => {
      console.log(rows);
    });
  });
}

// PoolCluster
{
  using cluster = mysql.createPoolCluster();

  cluster.add('MASTER', {});

  cluster.of('*').query('SELECT * FROM users', (err, rows) => {
    console.log(rows);
  });
}

@codecov
Copy link

codecov bot commented Feb 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.21%. Comparing base (a3178bf) to head (5863bc6).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4112      +/-   ##
==========================================
+ Coverage   90.15%   90.21%   +0.06%     
==========================================
  Files          86       86              
  Lines       13696    13740      +44     
  Branches     1645     1660      +15     
==========================================
+ Hits        12348    12396      +48     
+ Misses       1348     1344       -4     
Flag Coverage Δ
compression-0 89.44% <100.00%> (+0.06%) ⬆️
compression-1 90.19% <100.00%> (+0.06%) ⬆️
static-parser-0 87.81% <100.00%> (+0.06%) ⬆️
static-parser-1 88.57% <100.00%> (+0.06%) ⬆️
tls-0 89.62% <100.00%> (+0.06%) ⬆️
tls-1 89.99% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wellwelwel wellwelwel marked this pull request as ready for review February 23, 2026 13:22
@wellwelwel wellwelwel merged commit 1e612dc into sidorares:master Feb 23, 2026
89 checks passed
@wellwelwel wellwelwel deleted the dispose branch February 23, 2026 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How do you close connections???? Add connection.end() call to quickstart documentation

1 participant