gpt4 book ai didi

node.js - 什么时候应该在 NodeJS 中使用 MySQL 的连接池

转载 作者:搜寻专家 更新时间:2023-10-31 22:32:27 24 4
gpt4 key购买 nike

使用 Node-MySql 模块:

根据我的理解,多线程程序比单线程程序从连接池中受益更多。这是真的吗?

如果这个逻辑被证明是正确的,那么连接池在 Node.JS 应用程序中有益的场景是什么?

最佳答案

无论是单线程还是多线程,池在 allowing open connections to be reused 中仍然是有益的而不是被关闭后立即打开另一个:

When you are done with a connection, just call connection.release() and the connection will return to the pool, ready to be used again by someone else.

多线程的额外好处是池还可以管理多个并发连接:

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.

不过,需要明确的是,Node 是多线程的。它只是使用了一种不同于典型模型的模型——1 个执行 JavaScript 的“应用程序”线程和处理异步 I/O 的首当其冲的多个“worker”线程。

关于node.js - 什么时候应该在 NodeJS 中使用 MySQL 的连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17610793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com