Skip to content

Commit f498a40

Browse files
committed
Improve threadpool doc
1 parent a61ef1f commit f498a40

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

doc/api.md

+39-26
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ For installation information, see the [Node-oracledb Installation Instructions][
464464
- 16.1.3 [Net Service Names for Connection Strings](#tnsnames)
465465
- 16.1.4 [JDBC and Oracle SQL Developer Connection Strings](#notjdbc)
466466
- 16.2 [Connections, Threads, and Parallelism](#numberofthreads)
467-
- 16.2.1 [Parallelism on a Connection](#parallelism)
467+
- 16.2.1 [Connections and Worker Threads](#workerthreads)
468+
- 16.2.2 [Parallelism on Each Connection](#parallelism)
468469
- 16.3 [Connection Pooling](#connpooling)
469470
- 16.3.1 [Connection Pool Sizing](#conpoolsizing)
470471
- 16.3.2 [Connection Pool Closing and Draining](#conpooldraining)
@@ -1409,7 +1410,7 @@ concurrent statements on a connection by design. Also some application modules
14091410
may have the expectation that node-oracledb will handle any necessary
14101411
connection usage serialization.
14111412

1412-
For more discussion, see [Parallelism on a Connection](#parallelism).
1413+
For more discussion, see [Parallelism on Each Connection](#parallelism).
14131414

14141415
This property was added in node-oracledb 5.2.
14151416

@@ -9618,11 +9619,30 @@ const connection = await oracledb.getConnection(
96189619

96199620
### <a name="numberofthreads"></a> 16.2 Connections, Threads, and Parallelism
96209621

9621-
If you open more than four connections, such as via increasing
9622-
[`poolMax`](#proppoolpoolmax), you should increase the number of worker threads
9623-
available to node-oracledb. A thread pool that is too small can cause
9624-
connection requests to fail with the error *NJS-040: connection request
9625-
timeout* or *NJS-076: connection request rejected*.
9622+
To scale and optimize your applications it is useful to understand how
9623+
connections interact with Node.js worker threads, and to know that each
9624+
connection can only execute one database operation at a time.
9625+
9626+
#### <a name="workerthreads"></a> 16.2.1 Connections and Worker Threads
9627+
9628+
Node.js has four background worker threads by default (not to be confused with
9629+
the newer user space worker_threads module). If you open more than four
9630+
[standalone connections](#connectionhandling) or pooled connections, such as by
9631+
increasing [`poolMax`](#proppoolpoolmax), then you should increase the number
9632+
of worker threads available to node-oracledb.
9633+
9634+
A worker thread pool that is too small can cause a decrease in application
9635+
performance, can cause [deadlocks][22], or can cause connection requests to
9636+
fail with the error *NJS-040: connection request timeout* or *NJS-076:
9637+
connection request rejected*.
9638+
9639+
A Node.js worker thread is used by each connection to execute a database
9640+
statement. Each thread will wait until all [round-trips](#roundtrips) between
9641+
node-oracledb and the database for the statement are complete. When an
9642+
application handles a sustained number of user requests, and database
9643+
operations take some time to execute or the network is slow, then all available
9644+
threads may be held in use. This prevents other connections from beginning
9645+
work and stops Node.js from handling more user load.
96269646

96279647
The thread pool size should be equal to, or greater than, the maximum number of
96289648
connections. If the application does database and non-database work
@@ -9648,7 +9668,8 @@ Or, on Windows:
96489668
. . .
96499669
```
96509670

9651-
With these, you can start your application with `npm start`.
9671+
With these, you can start your application with `npm start`. This will allow
9672+
up to 10 connections to be actively excuting SQL statements in parallel.
96529673

96539674
On non-Windows platforms, the value can also be set inside the application. It
96549675
must be set prior to any asynchronous Node.js call that uses the thread pool:
@@ -9661,14 +9682,14 @@ process.env.UV_THREADPOOL_SIZE = 10
96619682
// ... rest of code
96629683
```
96639684

9664-
If you set `UV_THREADPOOL_SIZE` too late, the setting will be ignored and the
9665-
default thread pool size of 4 will still be used. Note that
9685+
If you set `UV_THREADPOOL_SIZE` too late in the application, or try to set it
9686+
this way on Windows, then the setting will be ignored and the default thread
9687+
pool size of 4 will still be used. Note that
96669688
[`pool.getStatistics()`](#poolgetstatistics) and
96679689
[`pool.logStatistics()`](#poollogstatistics) can only give the value of the
96689690
variable, not the actual size of the thread pool created. On Linux you can use
9669-
`pstack` to see how many threads are actually running. Note Node.js will
9670-
create a small number of threads in addition to the expected number of worker
9671-
threads.
9691+
`pstack` to see how many threads are actually running. Node.js will create a
9692+
small number of threads in addition to the expected number of worker threads.
96729693

96739694
The '[libuv][21]' library used by Node.js 12.5 and earlier limits the number of
96749695
threads to 128. In Node.js 12.6 onward the limit is 1024. You should restrict
@@ -9677,15 +9698,7 @@ i.e. [`poolMax`](#createpoolpoolattrspoolmax), to a value lower than
96779698
`UV_THREADPOOL_SIZE`. If you have multiple pools, make sure the sum of all
96789699
`poolMax` values is no larger than `UV_THREADPOOL_SIZE`.
96799700

9680-
Node.js worker threads executing database statements on a connection will wait
9681-
until [round-trips](#roundtrips) between node-oracledb and the database are
9682-
complete. When an application handles a sustained number of user requests, and
9683-
database operations take some time to execute or the network is slow, then all
9684-
available threads may be held in use. This prevents other connections from
9685-
beginning work and stops Node.js from handling more user load. Increasing the
9686-
number of worker threads may improve throughput and prevent [deadlocks][22].
9687-
9688-
#### <a name="parallelism"></a> 16.2.1 Parallelism on a Connection
9701+
#### <a name="parallelism"></a> 16.2.2 Parallelism on Each Connection
96899702

96909703
Oracle Database can only execute operations one at a time on each connection.
96919704
Examples of operations include `connection.execute()`,
@@ -10242,9 +10255,9 @@ blocking (for up to [`queueTimeout`](#propdbqueuetimeout) seconds) while
1024210255
waiting an available pooled connection. It lets you see when the pool is too
1024310256
small.
1024410257

10245-
You may also experience *NJS-040* or *NJS-076* errors if your application is not
10246-
correctly closing connections, or [UV_THREADPOOL_SIZE](#numberofthreads) is too
10247-
small.
10258+
You may also experience *NJS-040* or *NJS-076* errors if your application is
10259+
not correctly closing connections, or if [UV_THREADPOOL_SIZE](#numberofthreads)
10260+
is too small.
1024810261

1024910262
#### <a name="connpoolmonitor"></a> 16.3.5 Connection Pool Monitoring
1025010263

@@ -12245,7 +12258,7 @@ Connections can handle one database operation at a time. Other
1224512258
database operations will block. Structure your code to avoid starting
1224612259
parallel operations on a connection. For example avoid using
1224712260
`async.parallel` or `Promise.all()` which call each of their items in parallel,
12248-
see [Parallelism on a Connection](#parallelism).
12261+
see [Parallelism on Each Connection](#parallelism).
1224912262

1225012263
After all database calls on the connection complete, the application
1225112264
should use the [`connection.close()`](#connectionclose) call to

0 commit comments

Comments
 (0)