简体   繁体   中英

Knex.js connection error when rerunning the function

I'm using Knex to connect to an Azure database, run a query that returns the status of a database (COPYING/ONLINE). If I run this once, all is fine. But if I use a setInterval to rerun this (I want to know when the status changes from COPYING to ONLINE) I'm getting a connection error the second, and third, and.. time the function is called.

Here is my code

const knex = require('knex')({
  client: 'mssql',
  connection: {
    host: '***',
    user: '***',
    password: '***',
    options: { requestTimeout: 350000, encrypt: true },
  },
  pool: {
    min: 0,
    max: 15,
  },
});
async function copyStatus() {
  try {
    console.log('Running query');
    const status = await knex.raw(
      "SELECT name, state_desc FROM sys.databases WHERE name = 'Tide_QA_Dev_runtime' "
    );
    return status[0].state_desc;
    // console.log(status[0].state_desc);
  } catch (error) {
    console.log(error);
  } finally {
    console.log('Closing connection with database');
    await knex.destroy();
  }
}

function intervalFunc() {
  copyStatus().then(function (result) {
    if (result === 'ONLINE') {
      console.log('Database copy is done.');
    } else if (result === 'Database is still copying') {
      console.log('bezig');
    }
  });
}

setInterval(intervalFunc, 2000);

Here is my output

Closing connection with database
Database copy is done.
Running query
Error: Unable to acquire a connection
    at Client_MSSQL.acquireConnection (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/client.js:286:13)
    at Runner.ensureConnection (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/execution/runner.js:259:46)
    at Runner.run (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/execution/runner.js:30:30)
    at Raw.Target.then (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/builder-interface-augmenter.js:24:43)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
Closing connection with database
Running query
Error: Unable to acquire a connection
    at Client_MSSQL.acquireConnection (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/client.js:286:13)
    at Runner.ensureConnection (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/execution/runner.js:259:46)
    at Runner.run (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/execution/runner.js:30:30)
    at Raw.Target.then (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/builder-interface-augmenter.js:24:43)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
Closing connection with database```

It looks like the connection is made (see console log: Running query).
Any idea what's going on?

You should use code like below, it works for me.

const knex = require('knex')({
    client: 'mssql',
    connection: {
        // no tcp:
        server: 'j***2sqlserver.database.windows.net',
        user: 'j***2',
        password: 'J****0',
        database: 'yourdbname',
        port: 1433,
        options: { requestTimeout: 350000, encrypt: true },
    },
    pool: {
        min: 0,
        max: 15,
    },
});

My Test Result.

在此处输入图像描述

You should not call knex.destroy() if you are going to make more queries to the DB.

Move that knex.destroy() call to somewhere just before application is going to exit.

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