Skip to content

Commit d6bded1

Browse files
committed
Session tagging doc updates mostly around PL/SQL
1 parent 2bb05db commit d6bded1

File tree

1 file changed

+79
-36
lines changed

1 file changed

+79
-36
lines changed

doc/api.md

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,12 +2361,18 @@ connection](#getconnectiondbattrstag):
23612361
- When a Node.js `sessionCallback` function is used, then
23622362
`connection.tag` will be set to the value of the connection's actual
23632363
tag prior to invoking the callback. The callback can then set
2364-
connection state and alter `connection.tag` as desired.
2365-
2366-
- When a PL/SQL `sessionCallback` is used, then `connection.tag`
2367-
contains the value of the tag that was requested by
2368-
`pool.getConnection()`. The PL/SQL callback is expected to set the
2369-
state to match.
2364+
connection state and alter `connection.tag`, as desired, before the
2365+
connection is returned from `pool.getConnection()`.
2366+
2367+
- When a PL/SQL `sessionCallback` procedure is used, then after
2368+
`pool.getConnection()` returns, `connection.tag` contains a tag with
2369+
the same property values as the tag that was requested. The
2370+
properties may be in a different order. If `matchAnyTag` is *true*,
2371+
then `connection.tag` may contain other properties in addition to
2372+
the requested properties. Code after each `pool.getConnection()`
2373+
call mirroring the PL/SQL code may be needed so `connection.tag` can
2374+
be set to a value representing the session state changed in the
2375+
PL/SQL procedure.
23702376

23712377
##### Setting the tag
23722378

@@ -6550,6 +6556,8 @@ of setting session state if a previous user of a connection has
65506556
already set it. The caller of `pool.getConnection()` can always
65516557
assume the correct state is set.
65526558
6559+
###### <a name="sessionfixupnode"></a> Node.js Callback
6560+
65536561
This example sets two NLS settings in each pooled connection. They
65546562
are only set the very first time connections are established to the
65556563
database. The `requestedTag` parameter is ignored because it is only
@@ -6603,48 +6611,73 @@ session will be returned. If the optional `getConnection()` attribute
66036611
`matchAnyTag` is *true*, then a connection that has a different tag
66046612
may be returned.
66056613
6606-
The `sessionCallback` function is invoked before
6607-
`pool.getConnection()` returns if the requested tag is not identical
6608-
to the actual tag of the pooled connection. The best practice
6609-
recommendation is to set the tag in the callback function where the
6610-
callback changes session state. If required, a tag can be set anytime
6611-
prior to closing the connection. To clear a connection's tag set
6612-
`connection.tag` to an empty string.
6614+
The [`sessionCallback`](#createpoolpoolattrssessioncallback) function
6615+
is invoked before `pool.getConnection()` returns if the requested tag
6616+
is not identical to the actual tag of the pooled connection. The
6617+
callback can compare the requested tag with the current actual tag in
6618+
`connection.tag`. Any desired state change can be made to the
6619+
connection and `connection.tag` can be updated to record the change.
6620+
The best practice recommendation is to set the tag in the callback
6621+
function but, if required, a tag can be set anytime prior to closing
6622+
the connection. To clear a connection's tag set `connection.tag` to
6623+
an empty string.
6624+
6625+
You would use tagging where you want `pool.getConnection()` to return
6626+
a connection which has one of several different states. If all
6627+
connections should have the same state then you can simply set
6628+
`sessionCallback`, as shown [earlier](#sessionfixupnode), and not use
6629+
tagging. Also, it may not be worthwhile using huge numbers of
6630+
different tags or using tagging where connections are being
6631+
[dropped](#connectionclose) or recreated frequently since the chance
6632+
of `pool.getConnection()` returning an already initialized connection
6633+
with the requested tag could be low, so most `pool.getConnection()`
6634+
calls would return a connection needing its session reset, and tag
6635+
management will just add overhead.
66136636
66146637
When node-oracledb is using Oracle Client libraries 12.2 or later,
66156638
then node-oracledb uses 'multi-property tags' and the tag string must
66166639
be of the form of one or more "name=value" pairs separated by a
6617-
semi-colon, for example `"loc=uk;lang=cy"`. Oracle's underlying
6618-
'session pool' uses various heuristics to determine which connection
6619-
is returned to the application. Refer to the [multi-property tags
6620-
documentation][125]. The callback function can parse the requested
6621-
multi-property tag and compare it with the connection's actual
6622-
properties in [`connection.tag`](#propconntag) to determine what exact
6623-
state to set. The callback can set this state and update
6624-
`connection.tag` as required.
6640+
semi-colon, for example `"loc=uk;lang=cy"`. The Oracle [session
6641+
pool][6] used by node-oracledb has various heuristics to determine
6642+
which connection is returned to the application. Refer to the
6643+
[multi-property tags documentation][125]. The callback function can
6644+
parse the requested multi-property tag and compare it with the
6645+
connection's actual properties in [`connection.tag`](#propconntag) to
6646+
determine what exact state to set and what value to update
6647+
`connection.tag` to.
66256648
6626-
###### <a name="sessionfixupnode"></a> Node.js Callback
6649+
###### <a name="sessiontaggingnode"></a> Node.js Session Tagging Callback
66276650
66286651
This example Node.js callback function ensures the connection contains
66296652
valid settings for an application-specific "location=USA" property and
6630-
ignores any other properties that may be set:
6653+
ignores any other properties in the tag that represent session state
6654+
set by other parts of the application (not shown) that are using the
6655+
same pool:
66316656
66326657
```javascript
66336658
const sessionTag = "location=USA";
66346659

66356660
function initSession(connection, requestedTag, cb) {
6661+
connection.tag = "LOCATION=GB";
66366662
let seen = connection.tag ? connection.tag.split(";").includes(requestedTag) : false;
66376663
if (seen) {
66386664
cb()
66396665
} else {
66406666
connection.execute(
6641-
`alter session set nls_date_format = 'MM/DD/YY' nls_language = AMERICAN`,
6667+
`ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YY' NLS_LANGUAGE = AMERICAN`,
66426668
(err) => {
6643-
connection.tag = requestedTag; // Save the connection's new state.
6644-
// In a real app this should merge connection.tag and
6645-
// requestedTag to record all properties of the connection
6646-
cb);
6647-
}
6669+
// Update the tag the record the connection's new state
6670+
let k = requestedTag.substr(0, requestedTag.indexOf('=')+1);
6671+
if (connection.tag.indexOf(k) >= 0) {
6672+
// Update value of an existing, matching property in the tag
6673+
let re = new RegExp(k + "[^;]*");
6674+
connection.tag = connection.tag.replace(re, requestedTag);
6675+
} else {
6676+
// the requested property was not previously set in the tag
6677+
connection.tag = requestedTag + ';' + connection.tag;
6678+
}
6679+
cb();
6680+
});
66486681
}
66496682
}
66506683

@@ -6669,7 +6702,7 @@ try {
66696702
66706703
For runnable examples, see [`sessiontagging1.js`][127] and [`sessiontagging2.js`][128].
66716704
6672-
###### <a name="sessionfixupplsql"></a> PL/SQL Callback
6705+
###### <a name="sessiontaggingplsql"></a> PL/SQL Session Tagging Callback
66736706
66746707
When node-oracledb is using Oracle Client libraries 12.2 or later,
66756708
`sessionCallback` can be a string containing the name of a PL/SQL
@@ -6681,8 +6714,18 @@ non-DRCP connections, the PL/SQL callback will require a round-trip
66816714
from the application.
66826715
66836716
After a PL/SQL callback completes and `pool.getConnection()` returns,
6684-
[`connection.tag`](#propconntag) will have the value of the requested
6685-
tag, so ensure the PL/SQL callback sets all necessary state.
6717+
[`connection.tag`](#propconntag) will have the same property values as
6718+
the requested tag. The property order may be different. For example
6719+
you may request "USER_TZ=UTC;LANGUAGE=FRENCH" but `connection.tag` may
6720+
be "LANGUAGE=FRENCH;USER_TZ=UTC". When `matchAnyTag` is *true*, then
6721+
various heuristics are used to determine which connection in the pool
6722+
to use. See the [multi-property tags documentation][125]. Additional
6723+
properties may be present in `connection.tag`.
6724+
6725+
There is no direct way for Node.js to know if the PL/SQL procedure was
6726+
called or what session state it changed. After `pool.getConnection()`
6727+
returns, care must be taken to set `connection.tag` to an appropriate
6728+
value.
66866729
66876730
A sample PL/SQL callback procedure looks like:
66886731
@@ -6775,12 +6818,12 @@ try {
67756818
});
67766819
. . .
67776820
6778-
let conn = await pool.getConnection({tag: sessionTag});
6821+
let connection = await pool.getConnection({tag: sessionTag});
67796822
6780-
. . . // Use connection.
6781-
// The value of connection.tag will be sessionTag
6823+
. . . // The value of connection.tag will be sessionTag
6824+
// Use connection.
67826825
6783-
await conn.close();
6826+
await connection.close();
67846827
}
67856828
```
67866829

0 commit comments

Comments
 (0)