Search code examples
javascriptnode.jsoraclenode-oracledb

Why is there a pause at the end of my Node.js script when using node-oracledb 6 Thin mode?


When I run the following Node.js script with node-oracledb 6.0's default 'Thin' mode, I see a delay/pause of a few seconds before it finally terminates.

const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');

async function runApp() {

  const connection = await oracledb.getConnection(dbConfig);
  const result = await connection.execute(`select * from dual`);
  console.dir(result.rows, { depth: null });
  console.log('not closing connection');
  // console.log('closing connection');
  // await connection.close();
}

runApp();

Note that I do not explicitly close the connection here. When I include connection.close(), the pause/delay goes away.

To demonstrate this issue with/without closing the connection, I use the time command in my Linux machine:

$ time node t.js
[ [ 'X' ] ]
closing connection

real    0m0.249s
user    0m0.082s
sys 0m0.017s

$ time node t.js
[ [ 'X' ] ]
not closing connection

real    0m8.187s
user    0m0.092s
sys 0m0.017s

I did not see this delay in the earlier node-oracledb version, irrespective of whether I closed connections or not. What is the reason?


Solution

  • This behavior with node-oracledb 6.0's Thin mode is expected. It is due to the open connection reference needing to be cleaned up by the garbage collector.

    In node-oracledb 6.0's thin mode, the sockets are known to the event loop and will keep the application running until these sockets are closed. Internally there is a finalization registry that forces these sockets to be closed when the object holding them is garbage collected. However, in thick mode or the earlier node-oracledb versions, the sockets are held by the Oracle Client library. So, Node.js pays no attention to them and is quite happy to terminate with them still open!