Doubt to Close SQL Connection in NODE JS

0

Good day I have implemented the following logic:

router.get('/estudiante',(req,res,next) =>{
    //res.send("hola mundo");



        var sql = require("mssql");

        // config for your database
        var config = {
            user: 'martin',
            password: '1234',
            server: 'JMTABORDA-PC\SQLEXPRESS', 
            database: 'school' 


        };






        // connect to your database
        sql.connect(config, function (err) {

            if (err) console.log(err);

            // create Request object
            var request = new sql.Request();

            // query to the database and get the records
            request.query('select * from Student', function (err, recordset) {

                if (err) console.log(err)

                // send records as a response
                res.send(recordset);



            });



        });




});

By bringing it in via the URL, for example: link I am very good at listing all the students, but when I refresh the page again I get the following connection error:

  

Error: Global connection already exists. Call sql.close () first.       at Object.connect (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ mssql \ lib \ base.js: 1591: 31)       at router.get (C: \ Users \ marti \ Documents \ FinalProject \ src \ routes \ student.js: 23: 17)       at Layer.handle [as handle_request] (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ layer.js: 95: 5)       at next (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ route.js: 137: 13)       at Route.dispatch (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ route.js: 112: 3)       at Layer.handle [as handle_request] (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ layer.js: 95: 5)       at C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ index.js: 281: 22       at Function.process_params (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ index.js: 335: 12)       at next (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ index.js: 275: 10)       at Function.handle (C: \ Users \ marti \ Documents \ FinalProject \ node_modules \ express \ lib \ router \ index.js: 174: 3)

I tried to implement a sql.Close (); but it does not appear to me.

I would appreciate if you can help me thank you very much

    
asked by jmtaborda 13.05.2018 в 18:23
source

1 answer

0

Quoting the user: SW bams in English; recommends creating a connection pool as follows

new sql.ConnectionPool(config).connect().then(pool => {
  return pool.request().query("")
  }).then(result => {
    let rows = result.recordset
    res.setHeader('Access-Control-Allow-Origin', '*')
    res.status(200).json(rows);
    sql.close();
  }).catch(err => {
    res.status(500).send({ message: "${err}"})
    sql.close();
  });
});

Original question

User profile

bams

    
answered by 13.05.2018 в 18:58