<legend id='3Pn9a'><style id='3Pn9a'><dir id='3Pn9a'><q id='3Pn9a'></q></dir></style></legend>

    • <bdo id='3Pn9a'></bdo><ul id='3Pn9a'></ul>
    <i id='3Pn9a'><tr id='3Pn9a'><dt id='3Pn9a'><q id='3Pn9a'><span id='3Pn9a'><b id='3Pn9a'><form id='3Pn9a'><ins id='3Pn9a'></ins><ul id='3Pn9a'></ul><sub id='3Pn9a'></sub></form><legend id='3Pn9a'></legend><bdo id='3Pn9a'><pre id='3Pn9a'><center id='3Pn9a'></center></pre></bdo></b><th id='3Pn9a'></th></span></q></dt></tr></i><div id='3Pn9a'><tfoot id='3Pn9a'></tfoot><dl id='3Pn9a'><fieldset id='3Pn9a'></fieldset></dl></div>

    1. <small id='3Pn9a'></small><noframes id='3Pn9a'>

    2. <tfoot id='3Pn9a'></tfoot>

      node.js + mysql 连接池

      时间:2023-05-23

        • <legend id='92XPl'><style id='92XPl'><dir id='92XPl'><q id='92XPl'></q></dir></style></legend>

              <tbody id='92XPl'></tbody>
              <bdo id='92XPl'></bdo><ul id='92XPl'></ul>
              <i id='92XPl'><tr id='92XPl'><dt id='92XPl'><q id='92XPl'><span id='92XPl'><b id='92XPl'><form id='92XPl'><ins id='92XPl'></ins><ul id='92XPl'></ul><sub id='92XPl'></sub></form><legend id='92XPl'></legend><bdo id='92XPl'><pre id='92XPl'><center id='92XPl'></center></pre></bdo></b><th id='92XPl'></th></span></q></dt></tr></i><div id='92XPl'><tfoot id='92XPl'></tfoot><dl id='92XPl'><fieldset id='92XPl'></fieldset></dl></div>

              • <tfoot id='92XPl'></tfoot>

                <small id='92XPl'></small><noframes id='92XPl'>

                本文介绍了node.js + mysql 连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我试图弄清楚如何构建我的应用程序以最有效地使用 MySQL.我正在使用 node-mysql 模块.这里的其他线程建议使用连接池,所以我设置了一个小模块 mysql.js

                I'm trying to figure out how to structure my application to use MySQL most efficent way. I'm using node-mysql module. Other threads here suggested to use connection pooling so i set up a little module mysql.js

                var mysql = require('mysql');
                
                var pool  = mysql.createPool({
                    host     : 'localhost',
                    user     : 'root',
                    password : 'root',
                    database : 'guess'
                });
                
                exports.pool = pool;
                

                现在每当我想查询 mysql 我需要这个模块然后查询数据库

                Now whenever I want to query mysql I require this module and then query the databse

                var mysql = require('../db/mysql').pool;
                
                var test = function(req, res) {
                     mysql.getConnection(function(err, conn){
                         conn.query("select * from users", function(err, rows) {
                              res.json(rows);
                         })
                     })
                }
                

                这是个好方法吗?我真的找不到太多使用 mysql 连接的例子,除了非常简单的一个,所有的事情都在主 app.js 脚本中完成,所以我真的不知道什么是约定/最佳实践.

                Is this good approach? I couldn't really find too much examples of using mysql connections besides very simple one where everything is done in main app.js script so I don't really know what the convention / best practices are.

                我应该在每次查询后总是使用 connection.end() 吗?如果我在某处忘记了它怎么办?

                Should I always use connection.end() after each query? What if I forget about it somewhere?

                如何重写我的 mysql 模块的导出部分以仅返回一个连接,这样我就不必每次都编写 getConnection()?

                How to rewrite the exports part of my mysql module to return just a connection so I don't have to write getConnection() every time?

                推荐答案

                这是一个很好的方法.

                如果您只想获得连接,请将以下代码添加到池所在的模块中:

                If you just want to get a connection add the following code to your module where the pool is in:

                var getConnection = function(callback) {
                    pool.getConnection(function(err, connection) {
                        callback(err, connection);
                    });
                };
                
                module.exports = getConnection;
                

                你还是每次都要写getConnection.但是你可以在第一次得到它时将连接保存在模块中.

                You still have to write getConnection every time. But you could save the connection in the module the first time you get it.

                使用完毕后不要忘记结束连接:

                Don't forget to end the connection when you are done using it:

                connection.release();
                

                这篇关于node.js + mysql 连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:MySQL 根密码更改 下一篇:MySQL - IN() 中的 ORDER BY 值

                相关文章

              • <legend id='qEnDx'><style id='qEnDx'><dir id='qEnDx'><q id='qEnDx'></q></dir></style></legend>
              • <small id='qEnDx'></small><noframes id='qEnDx'>

              • <i id='qEnDx'><tr id='qEnDx'><dt id='qEnDx'><q id='qEnDx'><span id='qEnDx'><b id='qEnDx'><form id='qEnDx'><ins id='qEnDx'></ins><ul id='qEnDx'></ul><sub id='qEnDx'></sub></form><legend id='qEnDx'></legend><bdo id='qEnDx'><pre id='qEnDx'><center id='qEnDx'></center></pre></bdo></b><th id='qEnDx'></th></span></q></dt></tr></i><div id='qEnDx'><tfoot id='qEnDx'></tfoot><dl id='qEnDx'><fieldset id='qEnDx'></fieldset></dl></div>

                      <bdo id='qEnDx'></bdo><ul id='qEnDx'></ul>
                  1. <tfoot id='qEnDx'></tfoot>