|
| 1 | +/* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * You may not use the identified files except in compliance with the Apache |
| 6 | + * License, Version 2.0 (the "License.") |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * NAME |
| 19 | + * connectionpool.js |
| 20 | + * |
| 21 | + * DESCRIPTION |
| 22 | + * Shows connection pool usage. Connection pools are recommended |
| 23 | + * for applications that use a lot of connections for short periods. |
| 24 | + * |
| 25 | + * This example uses Node 8's async/await syntax. |
| 26 | + * |
| 27 | + * Other connection pool examples are in sessionfixup.js, webapp.js, |
| 28 | + * webapppromises.js and webappawait.js |
| 29 | + * |
| 30 | + * For a standalone connection example, see connect.js |
| 31 | + * |
| 32 | + * In some networks forced pool termination may hang unless you have |
| 33 | + * 'disable_oob=on' in sqlnet.ora, see |
| 34 | + * https://oracle.github.io/node-oracledb/doc/api.html#tnsadmin |
| 35 | + * |
| 36 | + *****************************************************************************/ |
| 37 | + |
| 38 | +const oracledb = require('oracledb'); |
| 39 | +const dbConfig = require('./dbconfig.js'); |
| 40 | + |
| 41 | +async function init() { |
| 42 | + try { |
| 43 | + // Create a connection pool which will later be accessed via the |
| 44 | + // pool cache as the 'default' pool. |
| 45 | + await oracledb.createPool({ |
| 46 | + user: dbConfig.user, |
| 47 | + password: dbConfig.password, |
| 48 | + connectString: dbConfig.connectString |
| 49 | + // edition: 'ORA$BASE', // used for Edition Based Redefintion |
| 50 | + // events: false, // whether to handle Oracle Database FAN and RLB events or support CQN |
| 51 | + // externalAuth: false, // whether connections should be established using External Authentication |
| 52 | + // homogeneous: true, // all connections in the pool have the same credentials |
| 53 | + // poolAlias: 'default', // set an alias to allow access to the pool via a name. |
| 54 | + // poolIncrement: 1, // only grow the pool by one connection at a time |
| 55 | + // poolMax: 4, // maximum size of the pool. Increase UV_THREADPOOL_SIZE if you increase poolMax |
| 56 | + // poolMin: 0, // start with no connections; let the pool shrink completely |
| 57 | + // poolPingInterval: 60, // check aliveness of connection if idle in the pool for 60 seconds |
| 58 | + // poolTimeout: 60, // terminate connections that are idle in the pool for 60 seconds |
| 59 | + // queueTimeout: 60000, // terminate getConnection() calls in the queue longer than 60000 milliseconds |
| 60 | + // sessionCallback: myFunction, // function invoked for brand new connections or by a connection tag mismatch |
| 61 | + // stmtCacheSize: 30 // number of statements that are cached in the statement cache of each connection |
| 62 | + }); |
| 63 | + console.log('Connection pool started'); |
| 64 | + |
| 65 | + // Now the pool is running, it can be used |
| 66 | + await dostuff(); |
| 67 | + |
| 68 | + } catch (err) { |
| 69 | + console.error('init() error: ' + err.message); |
| 70 | + } finally { |
| 71 | + await closePoolAndExit(); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +async function dostuff() { |
| 76 | + let connection; |
| 77 | + try { |
| 78 | + // Get a connection from the default pool |
| 79 | + connection = await oracledb.getConnection(); |
| 80 | + let sql = `SELECT sysdate FROM dual WHERE :b = 1`; |
| 81 | + let binds = [1]; |
| 82 | + let options = { outFormat: oracledb.OBJECT }; |
| 83 | + let result = await connection.execute(sql, binds, options); |
| 84 | + console.log(result); |
| 85 | + } catch (err) { |
| 86 | + console.error(err); |
| 87 | + } finally { |
| 88 | + if (connection) { |
| 89 | + try { |
| 90 | + // Put the connection back in the pool |
| 91 | + await connection.close(); |
| 92 | + } catch (err) { |
| 93 | + console.error(err); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +async function closePoolAndExit() { |
| 100 | + console.log('\nTerminating'); |
| 101 | + try { |
| 102 | + // Get the pool from the pool cache and close it when no |
| 103 | + // connections are in use, or force it closed after 10 seconds |
| 104 | + // If this hangs, you may need DISABLE_OOB=ON in a sqlnet.ora file |
| 105 | + await oracledb.getPool().close(10); |
| 106 | + console.log('Pool closed'); |
| 107 | + process.exit(0); |
| 108 | + } catch(err) { |
| 109 | + console.error(err.message); |
| 110 | + process.exit(1); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +process |
| 115 | + .once('SIGTERM', closePoolAndExit) |
| 116 | + .once('SIGINT', closePoolAndExit); |
| 117 | + |
| 118 | +init(); |
0 commit comments