22
22
* Listens for an HTTP request and returns an image queried from a BLOB column
23
23
* Also shows the connection pool's caching using a 'default' pool.
24
24
*
25
- * Use demo.sql to create the required table or do:
26
- * DROP TABLE mylobs;
27
- * CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
28
- *
29
- * Run lobinsert1.js to load an image before running this example.
30
- *
31
25
* Start the listener with 'node blobhttp.js' and then use a browser
32
26
* to load http://localhost:7000/getimage
33
27
*
34
28
* This example uses Node 8's async/await syntax.
35
29
*
36
30
*****************************************************************************/
37
31
38
- var url = require ( 'url' ) ;
39
- var http = require ( 'http' ) ;
40
- var oracledb = require ( 'oracledb' ) ;
41
- var dbConfig = require ( './dbconfig.js' ) ;
32
+ const url = require ( 'url' ) ;
33
+ const http = require ( 'http' ) ;
34
+ const oracledb = require ( 'oracledb' ) ;
35
+ const dbConfig = require ( './dbconfig.js' ) ;
36
+ const demoSetup = require ( './demosetup.js' ) ;
42
37
43
- var httpPort = 7000 ;
38
+ const httpPort = 7000 ;
44
39
45
40
// Main entry point. Creates a connection pool which becomes the
46
41
// 'default' pool, and then creates an HTTP server.
47
42
async function init ( ) {
48
43
try {
49
- await oracledb . createPool (
50
- {
51
- user : dbConfig . user ,
52
- password : dbConfig . password ,
53
- connectString : dbConfig . connectString
54
- } ) ;
44
+ await oracledb . createPool ( dbConfig ) ;
55
45
console . log ( 'Connection pool started' ) ;
56
46
47
+ // create the demo table
48
+ const connection = await oracledb . getConnection ( ) ;
49
+ await demoSetup . setupLobs ( connection , true ) ;
50
+ await connection . close ( ) ;
51
+
57
52
// Create HTTP server and listen on port httpPort
58
53
const server = http . createServer ( ) ;
59
54
server . on ( 'error' , ( err ) => {
@@ -63,7 +58,7 @@ async function init() {
63
58
handleRequest ( request , response ) ;
64
59
} ) ;
65
60
await server . listen ( httpPort ) ;
66
- console . log ( "Server running. Try requesting: http://localhost:" + httpPort + "/getimage" ) ;
61
+ console . log ( "Server is running. Try loading http://localhost:" + httpPort + "/getimage" ) ;
67
62
68
63
} catch ( err ) {
69
64
console . error ( 'init() error: ' + err . message ) ;
@@ -84,14 +79,14 @@ async function handleRequest(request, response) {
84
79
connection = await oracledb . getConnection ( ) ; // gets a connection from the 'default' connection pool
85
80
86
81
const result = await connection . execute (
87
- "SELECT b FROM mylobs WHERE id = :id" , // get the image
82
+ "SELECT b FROM no_lobs WHERE id = :id" , // get the image
88
83
{ id : 2 }
89
84
) ;
90
85
if ( result . rows . length === 0 ) {
91
- throw new Error ( "No results. Did you run lobinsert1.js? " ) ;
86
+ throw new Error ( "No data selected from table. " ) ;
92
87
}
93
88
94
- var lob = result . rows [ 0 ] [ 0 ] ;
89
+ const lob = result . rows [ 0 ] [ 0 ] ;
95
90
if ( lob === null ) {
96
91
throw new Error ( "BLOB was NULL" ) ;
97
92
}
@@ -117,6 +112,7 @@ async function handleRequest(request, response) {
117
112
118
113
} catch ( err ) {
119
114
console . error ( err ) ;
115
+ await closePoolAndExit ( ) ;
120
116
} finally {
121
117
if ( connection ) {
122
118
try {
@@ -137,9 +133,9 @@ async function closePoolAndExit() {
137
133
console . log ( '\nTerminating' ) ;
138
134
try {
139
135
// Get the pool from the pool cache and close it when no
140
- // connections are in use, or force it closed after 10 seconds
136
+ // connections are in use, or force it closed after 2 seconds
141
137
// If this hangs, you may need DISABLE_OOB=ON in a sqlnet.ora file
142
- await oracledb . getPool ( ) . close ( 10 ) ;
138
+ await oracledb . getPool ( ) . close ( 2 ) ;
143
139
console . log ( 'Pool closed' ) ;
144
140
process . exit ( 0 ) ;
145
141
} catch ( err ) {
0 commit comments