Skip to content

Request timeout problems once in a while #1735

@Cottin

Description

@Cottin

We're running a next.js application on a Windows Server using iisnode.

Everything works fine, but during every ~50th user session we experience: RequestError: Timeout: Request failed to complete in 15000ms. It's enough to be a hassle but not enough to be able to be reproduced.

Is there anything conceptually wrong with the way we're using a global connection pool? (Simplified code below)
Or is this kind of "once in a while" timeout problems common when using mssql with pools?

helpers.js

import mssql from 'mssql'

const sqlConfig = {
  ...
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 10000,
  },
  options: {
    trustServerCertificate: true,
  },
};

const mainSqlPool = new mssql.ConnectionPool(sqlConfig);
let sqlPool = null;

async function getSqlPool() {
  if (!sqlPool) {
    try {
      const tempPool = await mainSqlPool.connect();
      sqlPool = tempPool;
    } catch (err) {
      console.error('Failed to connect to SQL pool:', err);
      throw err;
    }
  }
  return sqlPool;
}

mainSqlPool.on('error', (err) => {
  console.error('SQL pool error', err);
  sqlPool = null;
});



export async function runQuery(query) {
  try {
    const pool = getSqlPool()
    const request = new mssql.Request(pool);
    const result = await request.query(query);
    return result;
  } catch (err) {
    console.error('err', err);
    throw err;
  }
}

api/createUser.js

export default handler = (req, res) => {
  return await runQuery("insert into UserTable (name) values ('John Doe')")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions