Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

420
Vistas
Node.Js MSSQL Query Timeout Expired

I am using Node Express API to run SQL queries to populate a dashboard of data. I am using the mssql-node package to do so. Sometimes it runs flawlessly, other times I get the following error:

[Error: [Microsoft][SQL Server Native Client 11.0]Query timeout expired]

I am creating a poolPromise with a connectionPool to the db, then I pass that object to my other controllers which run the specific queries to populate data. I run the server which initiates the db.js script and connects to MSSQL with a pool connection.

db.js:

// for connecting to sql server
const sql = require('mssql/msnodesqlv8'); 

// db config to connect via windows auth
const dbConfig = {
    driver: 'msnodesqlv8',
    connectionString: 'Driver={SQL Server Native Client 11.0};Server={my_server};Database={my_db};Trusted_Connection={yes};',
    pool: {
        idleTimeoutMillis: 60000
    }
};

// create a connectionpool object to pass to controllers
// this should keep a sql connection open indefinitely that we can query when the server is running
const poolPromise = new sql.ConnectionPool(dbConfig)
                    .connect()
                    .then(pool => {
                        console.log('Connected to MSSQL');
                        return pool;
                    })
                    .catch(err => console.log('Database Connection Failed! Bad Config: ', err))

module.exports = { sql, poolPromise };

An example of one of my controllers and how I use the poolPromise object is below. I currently have about 7 of these controllers that run their own specific query to populate a specific element on the dashboard. The performance of the queries each run within 1-10 seconds (depending on current server load, as I am querying an enterprise production server/db, this can vary). As I mentioned earlier, the queries run flawlessly sometimes and I have no issues, but at other times I do have issues. Is this a symptom of me querying from a shared production server? Is it preferred to query from a server that has less load? Or am I doing something in my code that could be improved?

const { sql, poolPromise } = require('../db');

// function to get data
const getData = async (req, res) => {    
    try {
        // create query parameters from user request
        let id= req.query.id;
        
        // create query from connectionPool
        let pool = await poolPromise;
        let qry = `
            select * from tbl where id = @Id
        `
        let data = await pool.request()
                                 .input('Id', sql.VarChar(sql.MAX), id)
                                 .query(qry);
        // send 200 status and return records
        res.status(200);
        res.send(data.recordset);
    } catch(err) {
        console.log('Error:');
        console.log(err);
        res.sendStatus(500);
    }
};

module.exports = { getData };
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda