当您在连接上调用 `release` 而不是 `destroy` 时?

When you call `release` instead `destroy` on connection?

我问的是 https://github.com/mysqljs/mysql。我有点疑惑destroyrelease在实践中有什么区别。考虑这样的代码:

pool.getConnection(function(err, conn)
{
  if (err)
    throw err;

  console.log("Connected");
  conn.release();
});

这将永远挂起。如果我将 release 切换为 destroy 程序终止。项目页面中的示例有这样一段代码:

connection.release();
// Don't use the connection here, it has been returned to the pool.

所以两者都像终止一样只是 release 没有终止。我的问题是 - 那么 release 有什么用?

在继续之前,请记住我以前从未使用过它,我只是阅读了文档,我认为文档中的区别是这样解释的:

Connections are lazily created by the pool. If you configure the pool to allow up to 100 connections, but only ever use 5 simultaneously, only 5 connections will be made. Connections are also cycled round-robin style, with connections being taken from the top of the pool and returning to the bottom.

而破坏是这个

This will cause an immediate termination of the underlying socket. Additionally destroy() guarantees that no more events or callbacks will be triggered for the connection.

所以我认为连接就像底层 "main" 连接池对象的对象实例。您创建连接并可以从中执行查询。当您释放连接时,该连接对象将被清空。这不是主要连接称为 "pool"。

当您使用发布时,软件仍然连接到底层数据库。当您使用 destroy 时,软件不再连接到数据库。

假设我有 5 个连接实例,查询如下

conn1 => select statement that takes 5 mins. 
conn2 => execute a procedure that takes 30 mins. 
conn3 => delete stuff under a min 
conn4 => nothing 
conn5 => nothing 

在文档中,很明显 pool 将连接排队,如果我按照与上面相同的顺序执行连接语句,conn 3 将在 5 + 30 分钟后执行。如果在第二次连接执行期间,用户点击取消怎么办?然后我会释放那​​个特定的连接,它是 conn2,但我仍然连接到数据库,所以 conn3 可以开始执行。

把它想象成 javascript 你可以向对象的原型添加东西,我们鼓励你这样做,你可以删除你添加的任何东西。原始对象仍然存在,即使您删除原型上的内容,原始对象也不会被破坏。

看起来,非终止状态是有意保持池和 "cycle" 连接。对于某些批处理作业,它并不那么重要。以下是 sidorares 的回答:https://github.com/mysqljs/mysql/issues/1486