3. Initializing node-oracledb — node-oracledb 6.8.0 documentation (original) (raw)

By default, node-oracledb runs in a ‘Thin’ mode which connects directly to Oracle Database. This mode does not need Oracle Client libraries. However, when the driver does use these libraries to communicate to Oracle Database, then node-oracledb is said to be in ‘Thick’ mode and has additional functionality available. See node-oracledb Thick Mode Architecture for the architecture diagram.

All connections in an application use the same mode. See Finding the node-oracledb Modeto verify which mode is in use.

3.1. Enabling node-oracledb Thick Mode

To change from the default Thin mode to the Thick mode:

  1. Oracle Client libraries must be available to handle communication to your database. These need to be installed separately, see Installing node-oracledb.
    Oracle Client libraries from one of the following can be used:
    • An Oracle Instant Client Basic or Basic Light package. This is generally the easiest if you do not already have Oracle software installed.
    • A full Oracle Client installation (installed by running the Oracle Universal installer runInstaller).
    • An Oracle Database installation, if Node.js is running on the same machine as the database.
      The Client library version does not always have to match the Oracle Database version.
  2. Your application must call the synchronous functionoracledb.initOracleClient() to load the client libraries. For example:
    const oracledb = require('oracledb');
    let clientOpts = {};
    if (process.platform === 'win32') {
    // Windows
    // If you use backslashes in the libDir string, you will
    // need to double them.
    clientOpts = { libDir: 'C:\oracle\instantclient_23_5' };
    } else if (process.platform === 'darwin' && process.arch === 'arm64') {
    // macOS ARM64
    clientOpts = { libDir: process.env.HOME + '/Downloads/instantclient_23_3' };
    }
    // else on other platforms like Linux the system library search path MUST always be
    // set before Node.js is started, for example with ldconfig or LD_LIBRARY_PATH.
    // enable node-oracledb Thick mode
    oracledb.initOracleClient(clientOpts);

More details and options are shown in the following sections:

Notes on calling initOracleClient()

3.1.1. Enabling node-oracledb Thick Mode on Windows

On Windows, the alternative ways to enable Thick mode are:

3.1.2. Enabling node-oracledb Thick Mode on macOS

On macOS, the alternative ways to enable Thick mode are:

On Linux and related platforms, enable Thick mode by callinginitOracleClient() without passing a libDir parameter.

const oracledb = require('oracledb'); oracledb.initOracleClient();

Oracle Client libraries are looked for in the operating system library search path, such as configured with ldconfig or set in the environment variable LD_LIBRARY_PATH. This must be configured prior to running the Node.js process. Web servers and other daemons commonly reset environment variables so using ldconfig is generally preferred instead. On some UNIX platforms, an OS specific equivalent such as LIBPATH or SHLIB_PATH is used instead of LD_LIBRARY_PATH.

If the libraries are not found in the system library search path, then libraries in $ORACLE_HOME/lib will be used. Note that the environment variable ORACLE_HOME should only ever be set when you have a full database installation or full client installation (such as installed with the Oracle GUI installer). It should not be set if you are using Oracle Instant Client. If being used, the ORACLE_HOME variable and other necessary variables should be set before starting Node.js. SeeOracle Environment Variables.

On Linux, node-oracledb Thick mode will not automatically load Oracle Client library files from the directory where the node-oracledb binary module is located. One of the above methods should be used instead.

Ensure that the Node.js process has directory and file access permissions for the Oracle Client libraries. OS restrictions may prevent the opening of Oracle Client libraries installed in unsafe paths, such as from a user directory. You may need to install the Oracle Client libraries under a directory like /opt or /usr/local.

3.1.4. Tracing Oracle Client Libraries Loading

To trace the loading of Oracle Client libraries, the environment variable DPI_DEBUG_LEVEL can be set to 64 before starting Node.js. For example, on Linux, you might use:

$ export DPI_DEBUG_LEVEL=64 $ node myapp.js 2> log.txt

On Windows you might set the variable like:

3.2. Optional Oracle Configuration Files

3.2.1. Optional Oracle Net Configuration Files

Optional Oracle Net configuration files may be read by node-oracledb. These files affect connections and applications. The common files are:

Table 3.1 Optional Oracle Net Configuration Files

Name Description
tnsnames.ora Contains Oracle Net Service names and Oracle Net options for databases that can be connected to, see Net Service Names for Connection Strings. This file is only needed for advanced configuration. Not needed if connection strings use the Easy Connect syntax. The Oracle Net documentation on tnsnames.ora has more information. From version 6.6 onwards, node-oracledb recognizes the IFILE parameter that is used in the tnsnames.ora file to embed custom network configuration files.
sqlnet.ora A configuration file controlling the network transport behavior. For example it can set call timeouts for high availability, or be used to encrypt network traffic, or be used to configure logging and tracing. The Oracle Net documentation on sqlnet.ora has more information. Many settings can alternatively be specified using Easy Connect syntax This file is only used in node-oracledb Thick mode. In the node-oracledb Thin mode, many settings can be defined in Easy Connect syntax, in getConnection() or createPool() calls, or in the tnsnames.ora file.

The documentation Connections and High Availabilitydiscusses some specific Oracle Net configuration options useful for node-oracledb applications.

See Using Optional Oracle Configuration Files to understand how node-oracledb locates the files.

3.2.2. Optional Oracle Client Configuration File

If the Oracle Client Libraries used by node-oracledb Thick mode are version 12, or later, then an optional oraaccess.xmlfile can be used to configure some behaviors of those libraries, such as statement caching and prefetching. This can be useful if the application cannot be altered. The file is read when node-oracledb starts. The file is read from the same directory as the Optional Oracle Net Configuration files.

Note

The oraaccess.xml files is only used in the node-oracledb Thick mode. See Enabling node-oracledb Thick Mode.

The following oraaccess.xml file sets the Oracle client‘prefetch’ value to 1000 rows. This value affects every SQL query in the application:

1000

Prefetching is a tuning feature, see Tuning Fetch Performance.

The oraaccess.xml file has other uses including:

Refer to the documentation on oraaccess.xmlfor more information.

See Using Optional Oracle Configuration Files to understand how node-oracledb locates the file.

3.2.3. Using Optional Oracle Configuration Files

If you use optional Oracle configuration files such as tnsnames.ora,sqlnet.ora or oraaccess.xml, then put the files in an accessible directory and follow the Thin or Thick mode instructions below.

The files should be in a directory accessible to Node.js, not on the database server host.

For node-oracledb Thin mode

In node-oracledb Thin mode, you must specify the directory that contains thetnsnames.ora file by either:

On Windows, if you use backslashes in the configDir string, you will need to double them.

Note

In Thin mode, you must explicitly set the directory because traditional “default” locations such as the Instant Client network/admin/subdirectory, $ORACLE_HOME/network/admin/, or$ORACLE_BASE/homes/XYZ/network/admin/ (in a read-only Oracle Database home) are not automatically looked in.

For node-oracledb Thick mode

In node-oracledb Thick mode, the directory containing the optional files can be explicitly specified or a default location will be used. Do one of:

3.3. Oracle Environment Variables for node-oracledb

Some common environment variables that influence node-oracledb are shown below. The variables that may be needed depend on how Node.js is installed, how you connect to the database, and what optional settings are desired. It is recommended to set Oracle variables in the environment before invoking Node.js, however they may also be set in application code as long as they are set before node-oracledb is first used. System environment variables likeLD_LIBRARY_PATH must be set before Node.js starts.

Table 3.2 Common Oracle Environment Variables supported by node-oracledb

Oracle Environment Variables Purpose Node-oracledb Mode
LD_LIBRARY_PATH The library search path for Linux and some UNIX platforms. Set this to the directory containing the Oracle Client libraries, for example /opt/oracle/instantclient_23_5 or $ORACLE_HOME/lib. The variable needs to be set in the environment before Node.js is invoked. The variable is not needed if the libraries are located by an alternative method, such as from running ldconfig. On some UNIX platforms, an OS specific equivalent such as LIBPATH or SHLIB_PATH is used instead of LD_LIBRARY_PATH. Thick
NLS_DATE_FORMAT, NLS_TIMESTAMP_FORMAT See Fetching Numbers and Dates as String. The variables are ignored if NLS_LANG is not set. Thick
NLS_LANG Determines the ‘national language support’ globalization options for node-oracledb. If not set, a default value will be chosen by Oracle. Note that node-oracledb will always uses the AL32UTF8 character set. See Globalization and National Language Support (NLS). Thick
NLS_NUMERIC_CHARACTERS See Fetching Numbers and Dates as String. The variables are ignored if NLS_LANG is not set. Thick
ORA_SDTZ The default session time zone, see Fetching Dates and Timestamps. Both
ORA_TZFILE The name of the Oracle time zone file to use. See Time Zone Files. Thick
ORACLE_HOME The directory containing the Oracle Database software. This directory must be accessible by the Node.js process. This variable should not be set if node-oracledb uses Oracle Instant Client. Thick
PATH The library search path for Windows should include the location where OCI.DLL is found. Not needed if you pass libDir when calling oracledb.initOracleClient(). Thick
TNS_ADMIN The location of the optional Oracle Net configuration files and Oracle Client configuration files, including tnsnames.ora, sqlnet.ora, and oraaccess.xml, if they are not in a default location. The configDir value in a call to oracledb.initOracleClient() overrides TNS_ADMIN. Both

3.3.1. Scripts for Setting the Default Environment in a Database Installation

If you are using Linux, and node-oracledb is being run on the same computer as the database, you can set required Oracle environment variables, such as ORACLE_HOME and LD_LIBRARY_PATH in your shell by executing:

source /usr/local/bin/oraenv

Or, if you are using Oracle Database XE 11.2, by executing:

source /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh

Make sure the Node.js process has directory and file access permissions for the Oracle libraries and other files. Typically the home directory of the Oracle software owner will need permissions relaxed.

Note

The ORACLE_HOME and LD_LIBRARY_PATH environment variables are only used in the node-oracledb Thick mode.

3.4. Other node-oracledb Thick Mode Initialization

The oracledb.initOracleClient() function allowsdriverName anderrorUrl attributes to be set. These are useful for applications whose end-users are not aware node-oracledb is being used. An example of setting the attributes is:

const oracledb = require('oracledb'); oracledb.initOracleClient({ driverName: 'My Great App : 3.1.4' errorUrl: 'https://example.com/MyInstallInstructions.html', });

The driverName value will be shown in Oracle Database views likeV$SESSION_CONNECT_INFO. The convention for driverName is to separate the product name from the product version by a colon and single space characters. If this attribute is not specified, then the value “node-oracledb thk : _version_” is used, see Finding the node-oracledb Mode.

The errorUrl string will be shown in the exception raised ifinitOracleClient() cannot load Oracle Client libraries. This allows applications that use node-oracledb in Thick mode to refer users to application-specific installation instructions. If this attribute is not set, then the node-oracledb installation instructionsare used.