简体   繁体   中英

nodejs mariadb connection not disconnecting

hi I am using nodejs to create a rest API

but I am facing a problem

first see code

var http = require('http');
var url = require('url');
var mariadb = require('mariadb');

    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        var db_conn  = mariadb.createPool({
            host:"localhost",
            user:"root",
            password:"",
            database:"database",
            connectionLimit:1
        });
        db_conn.getConnection().then(db=>{
            db.query("select * from array_languages").then(data => {
                db.end();
                res.write(JSON.stringify(data));
                res.end();
            });
        });
    }).listen(8080,"localhost");

this code is returning the data perfectly.

But every time I make a request the connection is still open despite the request is finished.

在此处输入图像描述

Expected Result

Normally what heppens, When ever I execute an script then after execution the MySql connection closes

I have tried to close the connection

by using conn.end()
but connection is still open

I am from PHP Background

In PHP if you request a page, after the execution all the connections will close automatically. but here I am facing something new.

How Can I close The connection after request finish?

I forget about this question. But I tried to solve it today.

Solved

I was trying to close the connection but instead of this I should have closed the connection Pool

Full Code

var http = require('http');
var url = require('url');
var mariadb = require('mariadb');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    var db_conn  = mariadb.createPool({
        host           : "localhost",
        user           : "root",
        password       : "",
        database       : "database",
        connectionLimit: 1
    });
    db_conn.getConnection().then(db=>{
        db.query("SELECT * FROM array_languages").then(data => {
            db_conn.end();
            db.end();
            res.write(JSON.stringify(data));
            res.end();
        });
    });
}).listen(8080,"localhost");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM