Skip to content

Commit b437c8b

Browse files
committed
Show serial number in session fixup example
1 parent cbd914a commit b437c8b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

examples/sessionfixup.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Error.stackTraceLimit = 50;
5151
const http = require('http');
5252
const oracledb = require('oracledb');
5353
const dbConfig = require('./dbconfig.js');
54+
5455
const httpPort = 7000;
5556

5657
// This example runs in both node-oracledb Thin and Thick modes.
@@ -76,19 +77,22 @@ if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') {
7677

7778
// initSession() will be invoked internally when each brand new pooled
7879
// connection is first used. Its callback function 'callbackFn' should be
79-
// invoked only when all desired session state has been set.
80-
// In this example, the requestedTag and actualTag parameters are
81-
// ignored. They would be valid if connection tagging was being used.
82-
// If you have multiple SQL statements to execute, put them in a
83-
// single, anonymous PL/SQL block for efficiency.
80+
// invoked only when all desired session state has been set. In this example,
81+
// the requestedTag parameter is ignored. They would be valid if connection
82+
// tagging was being used. If you have multiple SQL statements to execute, put
83+
// them in a single, anonymous PL/SQL block for efficiency.
8484
function initSession(connection, requestedTag, callbackFn) {
85+
8586
// Your session initialization code would be here. This example just queries
86-
// the session id to show that the callback is invoked once per session.
87+
// the session id and serial number to show that the callback is invoked once
88+
// per new session.
8789
connection.execute(
88-
`SELECT SYS_CONTEXT('USERENV','SID') FROM DUAL`,
90+
`SELECT UNIQUE sid||'-'||serial#
91+
FROM v$session_connect_info
92+
WHERE sid = SYS_CONTEXT('USERENV', 'SID')`,
8993
function(err, result) {
90-
const sid = result.rows[0][0]; // session id
91-
console.log(`initSession invoked for session ${sid}`);
94+
const sidSer = result.rows[0][0]; // session id and serial number
95+
console.log(`initSession invoked for session and serial number: ${sidSer}`);
9296
callbackFn();
9397
});
9498
}
@@ -125,9 +129,13 @@ async function handleRequest(request, response) {
125129
try {
126130
// Get a connection from the default connection pool
127131
connection = await oracledb.getConnection();
128-
const sql = `SELECT CURRENT_TIMESTAMP, SYS_CONTEXT('USERENV','SID') FROM DUAL`;
132+
133+
const sql = `SELECT UNIQUE CURRENT_TIMESTAMP, sid||'-'||serial#
134+
FROM v$session_connect_info
135+
WHERE sid = SYS_CONTEXT('USERENV', 'SID')`;
136+
129137
const result = await connection.execute(sql);
130-
console.log(`Query at time ${result.rows[0][0]} used session ${result.rows[0][1]}`);
138+
console.log(`Query at ${result.rows[0][0]} used session and serial number ${result.rows[0][1]}`);
131139
} catch (err) {
132140
console.error(err.message);
133141
} finally {

0 commit comments

Comments
 (0)