createConnection 如何与 mysql 中的 nodeJS 一起工作? [英] how does createConnection work with nodeJS in mysql?

查看:53
本文介绍了createConnection 如何与 mysql 中的 nodeJS 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

createConnection 有什么作用?

What does createConnection do?

var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

我正在使用 mysql 模块在 nodeJS 中编写应用程序.我有一些自己的模块,例如 authentication,它肯定需要数据库连接.问题是:如果我有多个模块使用此方法创建连接,它会每次为我创建一个新连接还是使用第一个?如果创建,它会在第一次加载我自己的模块时创建还是每次创建?哦,如果它创建它什么时候会被销毁?

I'm writing an application in nodeJS using mysql module. I have some own modules, for example authentication, which definetely needs DB connection. Question is: if I have multiple modules where I use this method to create the connection, will it create a new connection for me everytime or use the first one? If creates, it creates the first time it loads my own module or everytime? Oh, and if it creates when is it going to be destroyed?

这是我在 authentication 模块中的方式:

Here's how I have it in my authentication module:

var config = require('./config.js');
var mysql = require('mysql');
var connection = mysql.createConnection(config.connectionString);

exports.login = function() ...

我对模块和自己的模块的工作方式有一些基本的了解.

I have some basic understanding missings about how modules and own modules work.

感谢您的回答.

推荐答案

您可以在一个模块中创建一个连接池,然后在所有模块之间共享该池,只要调用 pool.getConnection()你需要.这可能比始终保持单个共享连接打开要好.

You can create a connection pool in one module and then share that pool across all your modules, calling pool.getConnection() whenever you need to. That might be better than keeping a single, shared connection open all the time.

我正在做的一个项目就是这样做的:

One project I'm working on does this:

utils/database.js

utils/database.js

var mysql = require('mysql');

var pool = mysql.createPool({
    connectionLimit: 100,
    host: 'localhost',
    user: 'xxxxx',
    password: 'yyyyy',
    database: 'zzzzz',
    debug: false
});

module.exports = pool

accounts.js

accounts.js

var express = require('express');
var router = express.Router();

var pool = require('./utils/database');

router.get('/', function(req, res) {

    pool.getConnection(function(err, connection) {

        // do whatever you want with your connection here

        connection.release();

    });
});

module.exports = router;

<小时>

我玩的另一种方式是这样的:


Another way I'm playing around with is like this:

utils/database.js

utils/database.js

var mysql = require('mysql');

var pool = mysql.createPool({
    connectionLimit: 100,
    host: 'localhost',
    user: 'xxxxx',
    password: 'yyyyy',
    database: 'zzzzz',
    debug: false
});

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

module.exports = getConnection;

accounts.js

accounts.js

var express = require('express');
var router = express.Router();

var createConnection = require('./utils/database');

router.get('/', function(req, res) {

    createConnection(function(err, connection) {

        // do whatever you want with your connection here

        connection.release();

    });
});

module.exports = router;

这篇关于createConnection 如何与 mysql 中的 nodeJS 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆