zoukankan      html  css  js  c++  java
  • mysql.createpool_nodejs连接mysql——createPool&createConnection区别

    createConnection 建立连接&关闭连接

    语法

    (1)createConnection方法创建连接对象(正式的说法:使用createConnection方法创建一个表示与mysql数据库服务器之间连接的connection对象)

    var connection = mysql.createConnection(options); 
    

    (2)用对象的connect方法建立连接。

    connection.connect(function(err) { *** }); 
    

    (3)关闭连接:connection对象的end方法和destory方法。

    connection.end(function(err) { *** }); 
    connection.destroy();
    

    完整示例

    var connection = mysql.createConnection({
        host: 'localhost',
        port: 3306,
        user: 'root',
        password: '123456',
        database: 'test'
    });
    
    // 连接
    connection.connect(function (err) {
        if (err) {
            console.log('[query] - :' + err);
            return;
        }
        console.log('[connection connect]  succeed!');
    });
    
    // 查询数据
    connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
        if (error) throw error;
        console.log('The solution is: ', results[0].solution);
    });
    
    //关闭连接
    connection.end(function (err) {
        if (err) {
            return;
        }
        console.log('[connection end] succeed!');
    });
    

    createPool 创建连接池

    在开发web应用程序时,连接池是一个很重要的概念。建立一个数据库连接所消耗的性能成本是很高的。在服务器应用程序中,如果为每一个接收到的客户端请求都建立一个或多个数据库连接,将严重降低应用程序性能。

    因此在服务器应用程序中通常需要为多个数据库连接创建并维护一个连接池,当连接不再需要时,这些连接可以缓存在连接池中,当接收到下一个客户端请求时,从连接池中取出连接并重新利用,而不需要再重新建立连接。

    语法

    (1)创建连接池 createPool方法

    var pool = mysql.createPool(optioins);
    

    options参数包含createConnection方法中可以使用的各种属性,除此之外还有以下属性:createConnection,waitForConnections,connectionLimit,queueLimit。

    (2)从连接池中取出连接。getConnection方法。如无连接可用则隐式的建立一个数据库连接。

    pool.getConnection(function(err,connection))
    

    回调函数中的err为错误对象,connection为获取到的连接对象。

    (3)当连接不再使用时,用connection对象的release方法将其归还到连接池中。

    connection.release();
    

    (4)当一个连接不再需要使用且需要从连接池中移除时,用connection对象的destroy方法。

    connection.destroy(); 
    

    连接移除后,连接池中的连接数减一。

    (5)当一个连接池不再需要使用时,用连接池对象的end方法关闭连接池。

    pool.end();
    

    完整示例

    var pool = mysql.createPool({
        host: 'localhost',
        port: 3306,
        user: 'root',
        password: '123456',
        database: 'test'
    });
    pool.getConnection(function(err, connection) {
        if(err){
            console.log("建立连接失败");
        } else {
            console.log("建立连接成功");
            console.log(pool._allConnections.length); //  1
            connection.query('select * from user', function(err, rows) {
                if(err) {
                    console.log("查询失败");
                } else {
                    console.log(rows);
                }
                // connection.destory();
                console.log(pool._allConnections.length);  // 0
            })
        }
        pool.end();
    })
    

    原文连接:https://www.cnblogs.com/xsilence/p/12444228.html

     
  • 相关阅读:
    嵌入式 VFS: Cannot open root device "mtdblock2" or unknown-block(2,0)
    嵌入式 hi3518x平台h264+g711a封装mp4代码demo
    嵌入式 十个最值得阅读学习的C开源项目代码
    嵌入式 arm平台ping域名指定ip小结
    嵌入式 busybox自带的tftp、telnet、ftp服务器
    嵌入式 hi3518平台检测网线是否插上
    嵌入式 hi3518平台获取网络环境中的ip、netmask、broadcast等信息
    jdbcUrl is required with driverClassName错误解决
    class path resource [processes/] cannot be resolved to URL because it does not exist
    SpringBoot2.0--- 多数据源配置
  • 原文地址:https://www.cnblogs.com/liangziaha/p/14573725.html
Copyright © 2011-2022 走看看