oracle / node-oracledb

Oracle Database driver for Node.js maintained by Oracle Corp.
http://oracle.github.io/node-oracledb/
Other
2.24k stars 1.07k forks source link

Short hang at script end unless connections are explicitly closed in node-oracledb 6 Thin mode #1558

Closed cjbj closed 6 months ago

cjbj commented 1 year ago

With node-oracledb 6's default 'Thin' mode make sure you close connections. If you don't, then you might see a pause of a few seconds before a script finally terminates. This is expected. It is due to the open connection reference needing to be cleaned up by the garbage collector.

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

async function run() {

  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');
  //await connection.close();
}

run();

Running this with/without closing the connection:

$ 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
ritschwumm commented 1 year ago

the same goes for using a connection pool - if you don't close it, the application will not exit (as early as expected?)

anthony-tuininga commented 1 year ago

Yes. A pool consists of a number of connections so any connections that have been created also need to be closed.

As for why thin mode exhibits this behavior and thick mode does not? In 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 -- as mentioned by Chris. In thick mode, however, 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!