当你调用' release '而不是' destroy ' on connection?

时间:2021-10-06 07:00:10

I am asking about https://github.com/mysqljs/mysql . I am a bit puzzled what is the difference between destroy and release in practice. Consider such code:

我问的是https://github.com/mysqljs/mysql。我有点困惑,在实践中破坏和释放的区别是什么。考虑这样的代码:

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

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

This will hang forever. If I switch release to destroy the program terminates. The example from the project page has such piece of code:

这将挂起,直到永远。如果我切换释放来破坏程序,程序就会终止。项目页面中的示例有这样一段代码:

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

So both are terminating-like only release is not terminating. My question is -- what is the use of release then?

所以两者都是终止——只有释放不是终止。我的问题是——那么发布有什么用呢?

2 个解决方案

#1


3  

Before you continue, keep in mind that I never used this before and I just read the documentation and I think the difference is explained in the documentation by this:

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

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.

连接是由池惰性创建的。如果您将池配置为最多允许100个连接,但只同时使用5个连接,那么将只创建5个连接。连接也是循环循环的样式,连接从池的顶部被取出并返回到底部。

Whereas destroy is this

而破坏这

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()保证不会为连接触发更多的事件或回调。

So I thought of the the connection as like an object instance of the underlying "main" connection pool object. You create connection and you can execute queries out of it. When you release a connection, that connection object is emptied. This is not the main connection referred as the "pool".

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

When you use release, software is still connected to the underlying database. When you use destroy, software is no longer connected to the database.

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

Lets say I have 5 connection instances and queries are follows

假设我有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 

In the documentation, it is clear that pool queues the connections and if I execute the connection statements in the same order as above, conn 3 will execute after 5 + 30 mins. What if during the second connection execution, user hits cancel? Then I would release that specific connection which is conn2 but I am still connected to the db so the conn3 can start executing.

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

Think of it as in javascript you can add stuff to the object's prototype, and you are encouraged to, and you can delete whatever you added. The original object still remains and even if you delete stuff on the prototype, original object is not destroyed.

想象一下,在javascript中,您可以向对象的原型添加内容,并且鼓励您这样做,并且您可以删除添加的内容。原始对象仍然保留,即使删除原型上的内容,原始对象也不会被销毁。

#2


0  

As it appears, non-terminating state is intentional to keep the pool alive, and "cycle" the connections. For some batch job it is not that important. Here is the answer by sidorares: https://github.com/mysqljs/mysql/issues/1486

当它出现时,非终止状态是有意保持池的存活,并“循环”连接。对于一些批量作业来说,这并不重要。以下是sidorares的答案:https://github.com/mysqljs/mysql/issues/1486

#1


3  

Before you continue, keep in mind that I never used this before and I just read the documentation and I think the difference is explained in the documentation by this:

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

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.

连接是由池惰性创建的。如果您将池配置为最多允许100个连接,但只同时使用5个连接,那么将只创建5个连接。连接也是循环循环的样式,连接从池的顶部被取出并返回到底部。

Whereas destroy is this

而破坏这

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()保证不会为连接触发更多的事件或回调。

So I thought of the the connection as like an object instance of the underlying "main" connection pool object. You create connection and you can execute queries out of it. When you release a connection, that connection object is emptied. This is not the main connection referred as the "pool".

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

When you use release, software is still connected to the underlying database. When you use destroy, software is no longer connected to the database.

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

Lets say I have 5 connection instances and queries are follows

假设我有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 

In the documentation, it is clear that pool queues the connections and if I execute the connection statements in the same order as above, conn 3 will execute after 5 + 30 mins. What if during the second connection execution, user hits cancel? Then I would release that specific connection which is conn2 but I am still connected to the db so the conn3 can start executing.

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

Think of it as in javascript you can add stuff to the object's prototype, and you are encouraged to, and you can delete whatever you added. The original object still remains and even if you delete stuff on the prototype, original object is not destroyed.

想象一下,在javascript中,您可以向对象的原型添加内容,并且鼓励您这样做,并且您可以删除添加的内容。原始对象仍然保留,即使删除原型上的内容,原始对象也不会被销毁。

#2


0  

As it appears, non-terminating state is intentional to keep the pool alive, and "cycle" the connections. For some batch job it is not that important. Here is the answer by sidorares: https://github.com/mysqljs/mysql/issues/1486

当它出现时,非终止状态是有意保持池的存活,并“循环”连接。对于一些批量作业来说,这并不重要。以下是sidorares的答案:https://github.com/mysqljs/mysql/issues/1486