30
30
*
31
31
* In production applications, set poolMin=poolMax (and poolIncrement=0)
32
32
*
33
- * This example requires node-oracledb 3 or later.
33
+ * This example requires node-oracledb 5 or later.
34
34
*
35
35
* This example uses Node 8's async/await syntax.
36
36
*
37
37
*****************************************************************************/
38
38
39
+ // On Windows and macOS, you can specify the directory containing the Oracle
40
+ // Client Libraries at runtime, or before Node.js starts. On other platforms
41
+ // the system library search path must always be set before Node.js is started.
42
+ // See the node-oracledb installation documentation.
43
+ // If the search path is not correct, you will get a DPI-1047 error.
44
+ //
45
+ // oracledb.initOracleClient({ libDir: 'C:\\instantclient_19_8' }); // Windows
46
+ // oracledb.initOracleClient({ libDir: '/Users/your_username/Downloads/instantclient_19_8' }); // macOS
47
+
39
48
// If you increase poolMax, you must increase UV_THREADPOOL_SIZE before Node.js
40
49
// starts its thread pool. If you set UV_THREADPOOL_SIZE too late, the value is
41
50
// ignored and the default size of 4 is used.
@@ -50,6 +59,9 @@ const demoSetup = require('./demosetup.js');
50
59
51
60
const httpPort = 7000 ;
52
61
62
+ // If additionally using Database Resident Connection Pooling (DRCP), then set a connection class:
63
+ // oracledb.connectionClass = 'MYAPPNAME';
64
+
53
65
// Main entry point. Creates a connection pool and an HTTP server
54
66
// that executes a query based on the URL parameter given.
55
67
// The pool values shown are the default values.
@@ -71,9 +83,9 @@ async function init() {
71
83
// poolTimeout: 60, // terminate connections that are idle in the pool for 60 seconds
72
84
// queueMax: 500, // don't allow more than 500 unsatisfied getConnection() calls in the pool queue
73
85
// queueTimeout: 60000, // terminate getConnection() calls queued for longer than 60000 milliseconds
74
- // sessionCallback: myFunction , // function invoked for brand new connections or by a connection tag mismatch
86
+ // sessionCallback: initSession , // function invoked for brand new connections or by a connection tag mismatch
75
87
// stmtCacheSize: 30, // number of statements that are cached in the statement cache of each connection
76
- // _enableStats: false // record pool usage statistics that can be output with pool ._logStats()
88
+ // _enableStats: false // record pool usage statistics that can be output with oracledb.getPool() ._logStats()
77
89
} ) ;
78
90
79
91
// create the demo table
@@ -97,18 +109,20 @@ async function init() {
97
109
}
98
110
}
99
111
112
+ // initSession() is configured by the pool sessionCallback property.
113
+ // It will be invoked internally when each brand new pooled connection
114
+ // is first used. See the sessionfixup.js and sessiontaggingX.js examples.
115
+ /*
116
+ function initSession(connection, requestedTag, cb) {
117
+ connection.execute(`ALTER SESSION SET ...'`, cb);
118
+ }
119
+ */
120
+
100
121
async function handleRequest ( request , response ) {
101
122
const urlparts = request . url . split ( "/" ) ;
102
123
const id = urlparts [ 1 ] ;
103
124
104
- htmlHeader (
105
- response ,
106
- "Banana Farmer Demonstration" ,
107
- "Example using node-oracledb driver"
108
- ) ;
109
-
110
125
if ( id == 'favicon.ico' ) { // ignore requests for the icon
111
- htmlFooter ( response ) ;
112
126
return ;
113
127
}
114
128
@@ -134,7 +148,12 @@ async function handleRequest(request, response) {
134
148
[ id ] // bind variable value
135
149
) ;
136
150
137
- displayResults ( response , result , id ) ;
151
+ displayResults (
152
+ response ,
153
+ "Banana Farmer Demonstration" ,
154
+ "Example using node-oracledb driver" ,
155
+ result ,
156
+ id ) ;
138
157
139
158
} catch ( err ) {
140
159
handleError ( response , "handleRequest() error" , err ) ;
@@ -148,24 +167,31 @@ async function handleRequest(request, response) {
148
167
}
149
168
}
150
169
}
151
- htmlFooter ( response ) ;
152
- }
153
-
154
- // Report an error
155
- function handleError ( response , text , err ) {
156
- if ( err ) {
157
- text += ": " + err . message ;
158
- }
159
- console . error ( text ) ;
160
- response . write ( "<p>Error: " + text + "</p>" ) ;
161
170
}
162
171
163
172
// Display query results
164
- function displayResults ( response , result , id ) {
173
+ function displayResults ( response , title , caption , result , id ) {
174
+
175
+ response . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
176
+ response . write ( "<!DOCTYPE html>" ) ;
177
+ response . write ( "<html>" ) ;
178
+ response . write ( "<head>" ) ;
179
+ response . write ( "<style>" +
180
+ "body {background:#FFFFFF;color:#000000;font-family:Arial,sans-serif;margin:40px;padding:10px;font-size:12px;text-align:center;}" +
181
+ "h1 {margin:0px;margin-bottom:12px;background:#FF0000;text-align:center;color:#FFFFFF;font-size:28px;}" +
182
+ "table {border-collapse: collapse; margin-left:auto; margin-right:auto;}" +
183
+ "td, th {padding:8px;border-style:solid}" +
184
+ "</style>\n" ) ;
185
+ response . write ( "<title>" + caption + "</title>" ) ;
186
+ response . write ( "</head>" ) ;
187
+ response . write ( "<body>" ) ;
188
+ response . write ( "<h1>" + title + "</h1>" ) ;
189
+
165
190
response . write ( "<h2>" + "Harvest details for farmer " + id + "</h2>" ) ;
191
+
166
192
response . write ( "<table>" ) ;
167
193
168
- // Column Title
194
+ // Column Titles
169
195
response . write ( "<tr>" ) ;
170
196
for ( let col = 0 ; col < result . metaData . length ; col ++ ) {
171
197
response . write ( "<th>" + result . metaData [ col ] . name + "</th>" ) ;
@@ -181,29 +207,20 @@ function displayResults(response, result, id) {
181
207
response . write ( "</tr>" ) ;
182
208
}
183
209
response . write ( "</table>" ) ;
184
- }
185
210
186
- // Prepare HTML header
187
- function htmlHeader ( response , title , caption ) {
188
- response . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
189
- response . write ( "<!DOCTYPE html>" ) ;
190
- response . write ( "<html>" ) ;
191
- response . write ( "<head>" ) ;
192
- response . write ( "<style>" +
193
- "body {background:#FFFFFF;color:#000000;font-family:Arial,sans-serif;margin:40px;padding:10px;font-size:12px;text-align:center;}" +
194
- "h1 {margin:0px;margin-bottom:12px;background:#FF0000;text-align:center;color:#FFFFFF;font-size:28px;}" +
195
- "table {border-collapse: collapse; margin-left:auto; margin-right:auto;}" +
196
- "td, th {padding:8px;border-style:solid}" +
197
- "</style>\n" ) ;
198
- response . write ( "<title>" + caption + "</title>" ) ;
199
- response . write ( "</head>" ) ;
200
- response . write ( "<body>" ) ;
201
- response . write ( "<h1>" + title + "</h1>" ) ;
211
+ response . write ( "</body>\n</html>" ) ;
212
+ response . end ( ) ;
213
+
202
214
}
203
215
204
- // Prepare HTML footer
205
- function htmlFooter ( response ) {
206
- response . write ( "</body>\n</html>" ) ;
216
+ // Report an error
217
+ function handleError ( response , text , err ) {
218
+ if ( err ) {
219
+ text += ": " + err . message ;
220
+ }
221
+ console . error ( text ) ;
222
+ response . writeHead ( 500 , { "Content-Type" : "text/html" } ) ;
223
+ response . write ( text ) ;
207
224
response . end ( ) ;
208
225
}
209
226
0 commit comments