Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Explicit Resource Management to oracledb objects #1631

Open
sosoba opened this issue Dec 5, 2023 · 2 comments
Open

Add Explicit Resource Management to oracledb objects #1631

sosoba opened this issue Dec 5, 2023 · 2 comments

Comments

@sosoba
Copy link

sosoba commented Dec 5, 2023

  1. Describe your new request in detail

TS/JS, like other programming languages, introduces a syntax for guaranteed resource release (using). Ex.:

await using pool = await oracledb.createPool({...});
await using connection = await pool.getConnection();
const { rows = [] } = await connection.execute( 'SELECT b FROM no_lobs WHERE id = :id', { id: 2 });
await using lob = rows[0][0];
lob.pipe(response);

which is equivalent to:

const pool = await oracledb.createPool({...});
try {
  const connection = await pool.getConnection();
  try {
    const { rows }= await connection.execute( 'SELECT b FROM no_lobs WHERE id = :id', { id: 2 });
    const lob = rows[0][0];
    try {
      lob.pipe(response);
    } finally {
      lob.destroy();
    }
  } finally {
    await connection.close();
  }
} finally {
  await pool.close();
}

I think it is worth expanding the library to support this solution.

  1. Give supporting information about tools.

The implementation involves adding aliased symbolic methods to close / destroy. See:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management
https://tc39.es/proposal-explicit-resource-management/

@sharadraju
Copy link
Member

Thanks @sosoba for the suggestion. We will be looking into this.

@sosoba
Copy link
Author

sosoba commented Nov 21, 2024

Where is very simple polyfil for this feature:
oracledb.d.ts:

export * from 'oracledb';

declare module 'oracledb' {
  export interface Pool {
    [Symbol.asyncDispose]: () => Promise<void>;
  }
  export interface Connection {
    [Symbol.asyncDispose]: () => Promise<void>;
  }
}

polyfil.js:

import oracledb from 'oracledb';

oracledb.Pool.prototype[Symbol.asyncDispose] = function () {
  return this.close();
};

oracledb.Connection.prototype[Symbol.asyncDispose] = function () {
  return this.close();
};

Lob doesn't need any rework - Node streams already have built-in support for explicit dispose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants