Skip to content

Commit 90a50dc

Browse files
committed
Correct use of close event in queryStream() samples and doc
1 parent 0249378 commit 90a50dc

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@
2020
records ([ODPI-C
2121
change](https://github.com/oracle/odpi/commit/4e80a81257ce6e1066f4f6242fed533eaed45753)).
2222

23+
- Corrected `queryStream()` documentation and examples to show the `'close'`
24+
event should be received before closing connections. If connections are
25+
closed on the `'end'` event, then significant C layer memory may be [held
26+
open](https://github.com/oracle/node-oracledb/issues/1173) until the garbage
27+
collector frees the associated JavaScript resource.
28+
2329
- Reverted the
2430
[`events`](https://oracle.github.io/node-oracledb/doc/api.html#propdbevents)
2531
default back to pre-4.0 behavior due to connection creation timeouts in some
2632
environments. It is now *false* again.
2733

2834
- Error changes:
2935

36+
- Ensured that `queryStream()` errors raised during close are emitted in the
37+
`'error'` event.
38+
3039
- Enforce only one of `connectString` or `connectionString` being used for
3140
connection.
3241

doc/api.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,11 +3703,11 @@ This function provides query streaming support. The parameters are
37033703
the same as [`execute()`](#execute) except a callback is not used.
37043704
Instead this function returns a stream used to fetch data.
37053705

3706-
Each row is returned as a `data` event. Query metadata is available
3707-
via a `metadata` event. The `end` event indicates the end of the
3708-
query results.
3709-
3710-
The connection must remain open until the stream is completely read.
3706+
Each row is returned as a `data` event. Query metadata is available via a
3707+
`metadata` event. The `end` event indicates the end of the query results. The
3708+
connection must remain open until the stream is completely read and the `close`
3709+
event received. Alternatively the Stream [`destroy()`][92] method can be used
3710+
to terminate a stream early.
37113711

37123712
For tuning, adjust the value of
37133713
[`oracledb.fetchArraySize`](#propdbfetcharraysize) or the
@@ -8841,22 +8841,23 @@ await rs.close();
88418841
Streaming of query results allows data to be piped to other streams,
88428842
for example when dealing with HTTP responses.
88438843

8844-
Use [`connection.queryStream()`](#querystream) to create a stream from
8845-
a top level query and listen for events. You can also call
8846-
[`connection.execute()`](#execute) and use
8847-
[`toQueryStream()`](#toquerystream) to return a stream from the
8848-
returned [ResultSet](#resultsetclass), an OUT bind REF CURSOR
8849-
ResultSet, or [Implicit Results](#implicitresults) ResultSet.
8844+
Use [`connection.queryStream()`](#querystream) to create a stream from a top
8845+
level query and listen for events. You can also call
8846+
[`connection.execute()`](#execute) and use [`toQueryStream()`](#toquerystream)
8847+
to return a stream from the returned [ResultSet](#resultsetclass), from an OUT
8848+
bind REF CURSOR ResultSet, or from [Implicit Results](#implicitresults)
8849+
ResultSets.
88508850

8851-
With streaming, each row is returned as a `data` event. Query
8852-
metadata is available via a `metadata` event. The `end` event
8853-
indicates the end of the query results.
8851+
With streaming, each row is returned as a `data` event. Query metadata is
8852+
available via a `metadata` event. The `end` event indicates the end of the
8853+
query results, however it is generally best to put end-of-fetch logic in the
8854+
`close` event.
88548855

88558856
Query results should be fetched to completion to avoid resource leaks, or the
8856-
Stream [`destroy()`][92] method can be used to terminate a stream early.
8857-
8858-
The connection must remain open until the stream is completely read
8859-
and any returned [Lob](#lobclass) objects have been processed.
8857+
Stream [`destroy()`][92] method can be used to terminate a stream early. When
8858+
fetching, the connection must remain open until the stream is completely read
8859+
and the `close` event received. Any returned [Lob](#lobclass) objects should
8860+
also be processed first.
88608861

88618862
The query stream implementation is a wrapper over the [ResultSet
88628863
Class](#resultsetclass). In particular, successive calls to
@@ -8879,7 +8880,11 @@ stream.on('data', function (data) {
88798880
});
88808881

88818882
stream.on('end', function () {
8882-
// release connection...
8883+
// all data has been fetched...
8884+
});
8885+
8886+
stream.on('close', function () {
8887+
// can now close connection... (Note: do not close connections on 'end')
88838888
});
88848889

88858890
stream.on('metadata', function (metadata) {

examples/selectstream.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ async function run() {
6969
});
7070

7171
stream.on('end', function() {
72-
// console.log("stream 'end' event");
72+
// console.log("stream 'end' event"); // all data has been fetched
73+
});
74+
75+
stream.on('close', function() {
76+
// console.log("stream 'close' event");
77+
// The underlying ResultSet has been closed, so the connection can now
78+
// be closed, if desired. Note: do not close connections on 'end'.
7379
resolve(rowcount);
7480
});
7581
});

lib/querystream.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ function QueryStream(resultSet) {
6767
// Using setImmediate to ensure that end event handlers are processed
6868
// before the destroy logic is invoked.
6969
setImmediate(function() {
70-
self._destroy(null, function() {});
70+
self._destroy(null, function(err) {
71+
if (err) {
72+
self.emit('error', err);
73+
}
74+
});
7175
});
7276
});
7377
}

0 commit comments

Comments
 (0)