Skip to content

Commit 4999982

Browse files
committed
Added drainTime to pool.close() PR #950 (Danilo Silva)
1 parent 0e8c05e commit 4999982

File tree

16 files changed

+573
-51
lines changed

16 files changed

+573
-51
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## node-oracledb v3.0.0-dev (DD Mon YYYY)
44

5+
- Added a `drainTime` argument to
6+
[`pool.close()`](https://github.com/oracle/node-oracledb/blob/master/doc/api.md#poolclose),
7+
allowing pools to be force-closed after a specified number of
8+
seconds. PR #950 (Danilo Silva).
9+
510
- Added a
611
[`connection.callTimeout`](https://oracle.github.io/node-oracledb/doc/api.html#propconncalltimeout)
712
property to interrupt long running database calls, available when

doc/api.md

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ limitations under the License.
4343
- [`SUBSCR_EVENT_TYPE_DEREG`](#oracledbconstantssubscription), [`SUBSCR_EVENT_TYPE_OBJ_CHANGE`](#oracledbconstantssubscription), [`SUBSCR_EVENT_TYPE_QUERY_CHANGE`](#oracledbconstantssubscription), [`SUBSCR_EVENT_TYPE_AQ`](#oracledbconstantssubscription), [`SUBSCR_GROUPING_CLASS_TIME`](#oracledbconstantssubscription), [`SUBSCR_GROUPING_TYPE_SUMMARY`](#oracledbconstantssubscription), [`SUBSCR_GROUPING_TYPE_LAST`](#oracledbconstantssubscription), [`SUBSCR_QOS_BEST_EFFORT`](#oracledbconstantssubscription), [`SUBSCR_QOS_DEREG_NFY`](#oracledbconstantssubscription), [`SUBSCR_QOS_QUERY`](#oracledbconstantssubscription), [`SUBSCR_QOS_RELIABLE`](#oracledbconstantssubscription), [`SUBSCR_QOS_ROWIDS`](#oracledbconstantssubscription)
4444
- 3.1.8 [Continuous Query Notification Constants](#oracledbconstantscqn)
4545
- [`CQN_OPCODE_ALL_OPS`](#oracledbconstantscqn), [`CQN_OPCODE_ALL_ROWS`](#oracledbconstantscqn), [`CQN_OPCODE_ALTER`](#oracledbconstantscqn), [`CQN_OPCODE_DELETE`](#oracledbconstantscqn), [`CQN_OPCODE_DROP`](#oracledbconstantscqn), [`CQN_OPCODE_INSERT`](#oracledbconstantscqn), [`CQN_OPCODE_UPDATE`](#oracledbconstantscqn)
46+
- 3.1.9 [Pool Status Constants](#oracledbconstantspool)
47+
- [`POOL_STATUS_OPEN`](#oracledbconstantspool), [`POOL_STATUS_DRAINING`](#oracledbconstantspool), [`POOL_STATUS_CLOSED`](#oracledbconstantspool)
4648
- 3.2 [Oracledb Properties](#oracledbproperties)
4749
- 3.2.1 [`autoCommit`](#propdbisautocommit)
4850
- 3.2.2 [`connectionClass`](#propdbconclass)
@@ -199,7 +201,8 @@ limitations under the License.
199201
- 6.1.8 [`poolTimeout`](#proppoolpooltimeout)
200202
- 6.1.9 [`queueRequests`](#proppoolqueuerequests)
201203
- 6.1.10 [`queueTimeout`](#proppoolqueueTimeout)
202-
- 6.1.11 [`stmtCacheSize`](#proppoolstmtcachesize)
204+
- 6.1.11 [`status`](#proppoolstatus)
205+
- 6.1.12 [`stmtCacheSize`](#proppoolstmtcachesize)
203206
- 6.2 [Pool Methods](#poolmethods)
204207
- 6.2.1 [`close()`](#poolclose)
205208
- 6.2.2 [`getConnection()`](#getconnectionpool)
@@ -655,6 +658,16 @@ Constant Name | Value |Description
655658
`oracledb.CQN_OPCODE_INSERT` | 2 | Set if the notifying transaction included inserts on the table
656659
`oracledb.CQN_OPCODE_UPDATE` | 4 | Set if the notifying transaction included updates on the table
657660

661+
#### <a name="oracledbconstantspool"></a> 3.1.9 Pool Status Constants
662+
663+
Constants for the connection [`pool.status`](#proppoolstatus) readonly attribute.
664+
665+
Constant Name | Value |Description
666+
--------------------------------|-------|---------------------------------------------------
667+
`oracledb.POOL_STATUS_OPEN` | 6000 | The connection pool is open.
668+
`oracledb.POOL_STATUS_DRAINING` | 6001 | The connection pool is being drained of in-use connections and will be force closed soon.
669+
`oracledb.POOL_STATUS_CLOSED` | 6002 | The connection pool has been closed.
670+
658671
### <a name="oracledbproperties"></a> 3.2 Oracledb Properties
659672

660673
The properties of the *Oracledb* object are used for setting up
@@ -1402,7 +1415,7 @@ pass the pool object through code.
14021415
See [Connection Pool Cache](#connpoolcache) for more details.
14031416

14041417
A pool should be terminated with the [`pool.close()`](#poolclose)
1405-
call, but only after all connections have been released.
1418+
call.
14061419

14071420
See [Connection Pooling](#connpooling) for more information about pooling.
14081421

@@ -3500,7 +3513,17 @@ the queue before the request is terminated.
35003513

35013514
See [`oracledb.queueTimeout`](#propdbqueuetimeout).
35023515

3503-
#### <a name="proppoolstmtcachesize"></a> 6.1.11 `pool.stmtCacheSize`
3516+
#### <a name="proppoolstatus"></a> 6.1.11 `pool.status`
3517+
3518+
```
3519+
readonly Number status
3520+
```
3521+
3522+
One of the [Pool Status Constants](#oracledbconstantspool) indicating
3523+
whether the pool is open, being drained of in-use connections, or has
3524+
been closed.
3525+
3526+
#### <a name="proppoolstmtcachesize"></a> 6.1.12 `pool.stmtCacheSize`
35043527

35053528
```
35063529
readonly Number stmtCacheSize
@@ -3519,27 +3542,58 @@ See [`oracledb.stmtCacheSize`](#propdbstmtcachesize).
35193542

35203543
Callback:
35213544
```
3522-
close(function(Error error){});
3545+
close([Number drainTime,] function(Error error){});
35233546
```
35243547
Promise:
35253548
```
3526-
promise = close();
3549+
promise = close([Number drainTime]);
35273550
```
35283551

35293552
##### Description
35303553

3531-
This call terminates the connection pool.
3532-
3533-
Any open connections should be released with [`connection.close()`](#connectionclose)
3534-
before `pool.close()` is called.
3554+
This call closes connections in the pool and terminates the connection
3555+
pool.
35353556

3536-
If the pool is in the [connection pool cache](#connpoolcache) it will be removed from the cache.
3557+
If a `drainTime` is not given, then any open connections should be
3558+
released with [`connection.close()`](#connectionclose) before
3559+
`pool.close()` is called, otherwise the pool close will fail and the
3560+
pool will remain open.
3561+
3562+
If a `drainTime` is specified, then any new `pool.getConnection()`
3563+
calls will fail. If connections are in use by the application, they
3564+
can continue to be used for the specified number of seconds, after
3565+
which the pool and all open connections are forcibly closed. Prior to
3566+
this time limit, if there are no connections currently "checked out"
3567+
from the pool with `getConnection()`, then the pool and any
3568+
connections that are idle in the pool are immediately closed.
3569+
Non-zero `drainTime` values are strongly recommended so applications
3570+
have the opportunity to gracefully finish database operations. A
3571+
`drainTime` of 0 may be used to close a pool and its connections
3572+
immediately.
3573+
3574+
In network configurations that drop (or in-line) out-of-band breaks,
3575+
forced pool termination may hang unless you have
3576+
[`DISABLE_OOB=ON`][301] in a `sqlnet.ora` file, see [Optional Client
3577+
Configuration Files](#tnsadmin).
3578+
3579+
When the pool is closed, it will be removed from the [connection pool
3580+
cache](#connpoolcache).
35373581

35383582
This method was added to node-oracledb 1.9, replacing the equivalent
35393583
alias `pool.terminate()`.
35403584

3585+
The `drainTime` parameter was added in node-oracledb 3.0.
3586+
35413587
##### Parameters
35423588

3589+
```
3590+
Number drainTime
3591+
```
3592+
3593+
The number of seconds before the pool and connections are force closed.
3594+
3595+
If `drainTime` is 0, the pool and its connections are closed immediately.
3596+
35433597
```
35443598
function(Error error)
35453599
```
@@ -4337,7 +4391,7 @@ still waiting in the queue.
43374391
43384392
##### Attribute Values
43394393
4340-
The `_logStats()` method also shows attribute values in effect for the pool:
4394+
The `_logStats()` method also shows attribute values of the pool:
43414395
43424396
Attribute |
43434397
--------------------------------------------|
@@ -4350,6 +4404,14 @@ Attribute |
43504404
[`poolPingInterval`](#propdbpoolpinginterval) |
43514405
[`stmtCacheSize`](#propdbstmtcachesize) |
43524406
4407+
##### Pool Status
4408+
4409+
The `_logStats()` method also shows the pool status:
4410+
4411+
Attribute |
4412+
----------------------------|
4413+
[`status`](#proppoolstatus) |
4414+
43534415
##### Related Environment Variables
43544416
43554417
One related environment variable is is shown by `_logStats()`:
@@ -8930,3 +8992,5 @@ When upgrading from node-oracledb version 2.0 to version 2.1:
89308992
[100]: https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-ABC7AE4D-64A8-4EA9-857D-BEF7300B64C3
89318993
[101]: https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-2BEF5482-CF97-4A85-BD90-9195E41E74EF
89328994
[102]: https://github.com/oracle/node-oracledb/issues/886
8995+
8996+
[301]: https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-4DD81A76-8D7D-4DEF-9DC1-77212C657AAF

examples/webapp.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
* accepts a URL parameter for the department ID, for example:
2828
* http://localhost:7000/90
2929
*
30+
* In some networks, pool termination may hang unless you have
31+
* 'disable_oob=on' in sqlnet.ora, see
32+
* https://oracle.github.io/node-oracledb/doc/api.html#tnsadmin
33+
*
3034
* Uses Oracle's sample HR schema. Scripts to create the HR schema
3135
* can be found at: https://github.com/oracle/db-sample-schemas
3236
*
@@ -201,14 +205,32 @@ function htmlFooter(response) {
201205
response.end();
202206
}
203207

208+
function closePoolAndExit() {
209+
console.log("\nTerminating");
210+
try {
211+
// get the pool from the pool cache and close it when no
212+
// connections are in use, or force it closed after 10 seconds
213+
var pool = oracledb.getPool();
214+
pool.close(10, function(err) {
215+
if (err)
216+
console.error(err);
217+
else
218+
console.log("Pool closed");
219+
process.exit(0);
220+
});
221+
} catch(err) {
222+
// Ignore getPool() error, which may occur if multiple signals
223+
// sent and the pool has already been removed from the cache.
224+
process.exit(0);
225+
}
226+
}
227+
204228
process
205229
.on('SIGTERM', function() {
206-
console.log("\nTerminating");
207-
process.exit(0);
230+
closePoolAndExit();
208231
})
209232
.on('SIGINT', function() {
210-
console.log("\nTerminating");
211-
process.exit(0);
233+
closePoolAndExit();
212234
});
213235

214236
init();

examples/webapppromises.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
* accepts a URL parameter for the department ID, for example:
2929
* http://localhost:7000/90
3030
*
31+
* In some networks, pool termination may hang unless you have
32+
* 'disable_oob=on' in sqlnet.ora, see
33+
* https://oracle.github.io/node-oracledb/doc/api.html#tnsadmin
34+
*
3135
* Uses Oracle's sample HR schema. Scripts to create the HR schema
3236
* can be found at: https://github.com/oracle/db-sample-schemas
3337
*
@@ -194,14 +198,32 @@ function htmlFooter(response) {
194198
response.end();
195199
}
196200

201+
function closePoolAndExit() {
202+
console.log("\nTerminating");
203+
try {
204+
// get the pool from the pool cache and close it when no
205+
// connections are in use, or force it closed after 10 seconds
206+
var pool = oracledb.getPool();
207+
pool.close(10, function(err) {
208+
if (err)
209+
console.error(err);
210+
else
211+
console.log("Pool closed");
212+
process.exit(0);
213+
});
214+
} catch(err) {
215+
// Ignore getPool() error, which may occur if multiple signals
216+
// sent and the pool has already been removed from the cache.
217+
process.exit(0);
218+
}
219+
}
220+
197221
process
198222
.on('SIGTERM', function() {
199-
console.log("\nTerminating");
200-
process.exit(0);
223+
closePoolAndExit();
201224
})
202225
.on('SIGINT', function() {
203-
console.log("\nTerminating");
204-
process.exit(0);
226+
closePoolAndExit();
205227
});
206228

207229
init();

lib/oracledb.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,18 @@ function extend(oracledb) {
616616
value: 4002,
617617
enumerable: true
618618
},
619+
POOL_STATUS_OPEN: {
620+
value: 6000,
621+
enumerable: true
622+
},
623+
POOL_STATUS_DRAINING: {
624+
value: 6001,
625+
enumerable: true
626+
},
627+
POOL_STATUS_CLOSED: {
628+
value: 6002,
629+
enumerable: true
630+
},
619631
Promise: {
620632
value: global.Promise,
621633
enumerable: true,

0 commit comments

Comments
 (0)