How does node mysql2/promise 's connection pool release connections?

connection pooling using mysql2/promise because the connection is not released, finally mysql will report a Can"t create more than max_prepared_stmt_count statements error, so how does it release the connection?
my code looks like this
1. Create a connection

const mysql = require("mysql2/promise")
 var connection =  mysql.createPool(mysqlConfig)
  global.connection = connection

2. Mount the link to ctx

app.use(async (ctx, next) => {

  ctx.mysqlConection = global.connection
  // console.log(global.connection, "99999999999")
  ctx.controller = controller
  ctx.service = service
  ctx.are = are
  ctx.iz = iz
  ctx.rules = rules.rules
  ctx.model = mongodb.mongoose.models;
  await next()
  ctx.mysqlConection.end()

})```

3. controller
findUv: async function (ctx) {
    let condition = ctx.request.body
    let sql = `SELECT count AS uv FROM gugudai_count ORDER BY id ASC LIMIT 1`
    console.log("******************2",ctx.mysqlConection.getConnection)
 
    const [rows, fields] = await ctx.mysqlConection.execute(sql)
    // let conne=await ctx.mysqlConection.getConnection;
    // console.log(conne)
    return { rows }
},
Mar.12,2021

after reading your reply, take a look at the official website API,. Your writing may be fine in other packages, but it does not conform to the implementation of mysql2 . For example, links are not created at first, but are created on demand and need to be recycled.

// For pool initialization, see above
pool.getConnection(function(conn) {
   // Do something with the connection
   conn.query(/* ... */);
   // Don't forget to release the connection when finished!
   pool.releaseConnection(conn);
})

mysql2

The idea above

should be the number of concurrent requests.
1. The link has not been finished before waiting for the next request with await. Then there are multiple requests using a single mysql link (in this case, a global variable)
2. The singleton pattern gives you the same experience as global variables, but in fact singletons are different from variables. Singleton classes always occupy the same memory address, and requests 1 and 2 also use the same singleton class.
3. It can be simply understood that if you are a global variable, then request 1 should also use global for select

when using global for select, and No.2 should also use global for select

Menu