@@ -2361,12 +2361,18 @@ connection](#getconnectiondbattrstag):
2361
2361
- When a Node.js ` sessionCallback ` function is used, then
2362
2362
` connection.tag ` will be set to the value of the connection's actual
2363
2363
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.
2370
2376
2371
2377
##### Setting the tag
2372
2378
@@ -6550,6 +6556,8 @@ of setting session state if a previous user of a connection has
6550
6556
already set it. The caller of ` pool .getConnection ()` can always
6551
6557
assume the correct state is set.
6552
6558
6559
+ ###### <a name="sessionfixupnode"></a> Node.js Callback
6560
+
6553
6561
This example sets two NLS settings in each pooled connection. They
6554
6562
are only set the very first time connections are established to the
6555
6563
database. The ` requestedTag` parameter is ignored because it is only
@@ -6603,48 +6611,73 @@ session will be returned. If the optional `getConnection()` attribute
6603
6611
` matchAnyTag` is *true*, then a connection that has a different tag
6604
6612
may be returned.
6605
6613
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.
6613
6636
6614
6637
When node-oracledb is using Oracle Client libraries 12.2 or later,
6615
6638
then node-oracledb uses 'multi-property tags' and the tag string must
6616
6639
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 .
6625
6648
6626
- ###### <a name="sessionfixupnode "></a> Node.js Callback
6649
+ ###### <a name="sessiontaggingnode "></a> Node.js Session Tagging Callback
6627
6650
6628
6651
This example Node.js callback function ensures the connection contains
6629
6652
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:
6631
6656
6632
6657
` ` ` javascript
6633
6658
const sessionTag = " location=USA" ;
6634
6659
6635
6660
function initSession (connection , requestedTag , cb ) {
6661
+ connection .tag = " LOCATION=GB" ;
6636
6662
let seen = connection .tag ? connection .tag .split (" ;" ).includes (requestedTag) : false ;
6637
6663
if (seen) {
6638
6664
cb ()
6639
6665
} else {
6640
6666
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` ,
6642
6668
(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
+ });
6648
6681
}
6649
6682
}
6650
6683
@@ -6669,7 +6702,7 @@ try {
6669
6702
6670
6703
For runnable examples, see [` sessiontagging1 .js ` ][127] and [` sessiontagging2 .js ` ][128].
6671
6704
6672
- ###### <a name="sessionfixupplsql "></a> PL/SQL Callback
6705
+ ###### <a name="sessiontaggingplsql "></a> PL/SQL Session Tagging Callback
6673
6706
6674
6707
When node-oracledb is using Oracle Client libraries 12.2 or later,
6675
6708
` 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
6681
6714
from the application.
6682
6715
6683
6716
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.
6686
6729
6687
6730
A sample PL/SQL callback procedure looks like:
6688
6731
@@ -6775,12 +6818,12 @@ try {
6775
6818
});
6776
6819
. . .
6777
6820
6778
- let conn = await pool.getConnection({tag: sessionTag});
6821
+ let connection = await pool.getConnection({tag: sessionTag});
6779
6822
6780
- . . . // Use connection.
6781
- // The value of connection.tag will be sessionTag
6823
+ . . . // The value of connection.tag will be sessionTag
6824
+ // Use connection.
6782
6825
6783
- await conn .close();
6826
+ await connection .close();
6784
6827
}
6785
6828
```
6786
6829
0 commit comments