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

标签 mysql node.js node-mysql

我问的是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.

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

当您使用发布时,软件仍然连接到底层数据库。当您使用 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 

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

将其视为在 javascript 中,您可以向对象的原型(prototype)添加内容,我们鼓励您这样做,并且您可以删除添加的任何内容。原始对象仍然保留,即使您删除原型(prototype)上的内容,原始对象也不会被破坏。

关于mysql - 当您在连接时调用 `release` 而不是 `destroy` 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38490440/

相关文章:

MYSQL SELECT语句新手

php - 文章表上传图片后存储image_id

php - 在两个不同位置获取相同的随机生成值

node.js - 蒙戈 : add fields with count of how many times another field appears

mysql - Node.JS 和 MySQL - 查询锁定并且执行速度极其缓慢

mysql - 如果范围大小大于分区数量,分区修剪不起作用吗?

node.js - Angular 4 中的数组推送导致浏览器卡住/卡住。如何解决此问题?

node.js - 在 Require.js 优化器中打包 *.html 模板?

mysql - 如何获得完整的 JSON 而无需在 node-mysql 中重复键?

mysql - 通过 Node.js 将重复键更新上的行批量插入到 mysql 中,同时还执行 mysql 函数