|
| 1 | +/* Copyright (c) 2023, Oracle and/or its affiliates. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * This software is dual-licensed to you under the Universal Permissive License |
| 6 | + * (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 7 | + * 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 8 | + * either license. |
| 9 | + * |
| 10 | + * If you elect to accept the software under the Apache License, Version 2.0, |
| 11 | + * the following applies: |
| 12 | + * |
| 13 | + * Licensed under the Apache License, Version 2.0 (the `License`); |
| 14 | + * you may not use this file except in compliance with the License. |
| 15 | + * You may obtain a copy of the License at |
| 16 | + * |
| 17 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 18 | + * |
| 19 | + * Unless required by applicable law or agreed to in writing, software |
| 20 | + * distributed under the License is distributed on an `AS IS` BASIS, |
| 21 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | + * See the License for the specific language governing permissions and |
| 23 | + * limitations under the License. |
| 24 | + * |
| 25 | + * NAME |
| 26 | + * 276. deadConnDetection.js |
| 27 | + * |
| 28 | + * DESCRIPTION |
| 29 | + * Dead connection detection |
| 30 | + * |
| 31 | + *****************************************************************************/ |
| 32 | +'use strict'; |
| 33 | + |
| 34 | +const oracledb = require('oracledb'); |
| 35 | +const assert = require('assert'); |
| 36 | +const dbConfig = require('./dbconfig.js'); |
| 37 | + |
| 38 | +describe('276. deadConnDetection.js', function() { |
| 39 | + |
| 40 | + let dbaConn = null; |
| 41 | + before(async function() { |
| 42 | + dbaConn = await oracledb.getConnection({ |
| 43 | + user : dbConfig.test.DBA_user, |
| 44 | + password : dbConfig.test.DBA_password, |
| 45 | + connectString : dbConfig.connectString, |
| 46 | + privilege : oracledb.SYSDBA, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + after(async function() { |
| 51 | + await dbaConn.close(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('276.1 dead connection detection in pool', async function() { |
| 55 | + const pool = await oracledb.createPool({ |
| 56 | + ...dbConfig, |
| 57 | + poolMin: 2, |
| 58 | + poolMax: 2, |
| 59 | + poolIncrement: 2 |
| 60 | + }); |
| 61 | + |
| 62 | + // acquire connections from the pool and kill all the sessions |
| 63 | + const conn1 = await pool.getConnection(); |
| 64 | + const conn2 = await pool.getConnection(); |
| 65 | + const connections = [conn1, conn2]; |
| 66 | + |
| 67 | + for (const conn of connections) { |
| 68 | + const result = await conn.execute(`select dbms_debug_jdwp.current_session_id, |
| 69 | + dbms_debug_jdwp.current_session_serial from dual`); |
| 70 | + const sid = result.rows[0][0]; |
| 71 | + const serial = result.rows[0][1]; |
| 72 | + const sql = `alter system kill session '${sid},${serial}'`; |
| 73 | + await dbaConn.execute(sql); |
| 74 | + await conn.close(); |
| 75 | + } |
| 76 | + |
| 77 | + assert.strictEqual(pool.connectionsInUse, 0); |
| 78 | + assert.strictEqual(pool.connectionsOpen, 2); |
| 79 | + |
| 80 | + // when try to re-use the killed sessions error will be raised; |
| 81 | + // release all such connections |
| 82 | + for (let i = 0; i < connections.length; i++) { |
| 83 | + connections[i] = await pool.getConnection(); |
| 84 | + } |
| 85 | + for (const conn of connections) { |
| 86 | + await assert.rejects( |
| 87 | + async () => await conn.execute(`select user from dual`), |
| 88 | + /NJS-500:/ |
| 89 | + ); |
| 90 | + await conn.close(); |
| 91 | + } |
| 92 | + |
| 93 | + assert.strictEqual(pool.connectionsInUse, 0); |
| 94 | + assert.strictEqual(pool.connectionsOpen, 0); |
| 95 | + await pool.close(0); |
| 96 | + }); |
| 97 | + |
| 98 | + it('276.2 test case to check after handling error (terminated session) pool', async function() { |
| 99 | + const config = { |
| 100 | + ...dbConfig, |
| 101 | + poolMin: 2, |
| 102 | + poolMax: 2, |
| 103 | + poolIncrement: 2 |
| 104 | + }; |
| 105 | + |
| 106 | + let pool; |
| 107 | + let connection = await oracledb.getConnection(dbConfig); |
| 108 | + let serverVersion = connection.oracleServerVersion; |
| 109 | + /* |
| 110 | + Minimum pool connection in case of Dead Connection Detection (DCD) |
| 111 | + is not backported in 21C database |
| 112 | + */ |
| 113 | + if (serverVersion >= 2100000000 && serverVersion <= 2190000000) return; |
| 114 | + await connection.close(); |
| 115 | + |
| 116 | + pool = await oracledb.createPool(config); |
| 117 | + const conn1 = await pool.getConnection(); |
| 118 | + const conn2 = await pool.getConnection(); |
| 119 | + assert.strictEqual(pool.connectionsInUse, 2); |
| 120 | + assert.strictEqual(pool.connectionsOpen, 2); |
| 121 | + const connections = [conn1, conn2]; |
| 122 | + for (const conn of connections) { |
| 123 | + const result = await conn.execute(`select dbms_debug_jdwp.current_session_id, |
| 124 | + dbms_debug_jdwp.current_session_serial from dual`); |
| 125 | + const sid = result.rows[0][0]; |
| 126 | + const serial = result.rows[0][1]; |
| 127 | + const sql = `alter system kill session '${sid},${serial}'`; |
| 128 | + await dbaConn.execute(sql); |
| 129 | + await conn.close(); |
| 130 | + } |
| 131 | + assert.strictEqual(pool.connectionsInUse, 0); |
| 132 | + assert.strictEqual(pool.connectionsOpen, 2); |
| 133 | + for (let i = 0; i < 2 * connections.length; i++) { |
| 134 | + const conn = await pool.getConnection(); |
| 135 | + if (i < 2) { |
| 136 | + await assert.rejects( |
| 137 | + async () => await conn.execute(`select user from dual`), |
| 138 | + /NJS-500:/ |
| 139 | + ); |
| 140 | + await conn.close(); |
| 141 | + /* The minimum connection in the pool was not maintained |
| 142 | + in case "while doing acquire connection, if validation fails, |
| 143 | + and many connections in the freeConnection list got popped out |
| 144 | + due to validation failure, the minimum connection is not maintained". |
| 145 | + */ |
| 146 | + assert.strictEqual(pool.connectionsInUse, 0); |
| 147 | + |
| 148 | + if (i == 0) assert.strictEqual(pool.connectionsOpen, 1); |
| 149 | + else assert.strictEqual(pool.connectionsOpen, 0); |
| 150 | + } else { |
| 151 | + await conn.execute(`select user from dual`); |
| 152 | + await conn.close(); |
| 153 | + assert.strictEqual(pool.connectionsInUse, 0); |
| 154 | + assert.strictEqual(pool.connectionsOpen, 2); |
| 155 | + } |
| 156 | + } |
| 157 | + await pool.close(0); |
| 158 | + }); |
| 159 | +}); |
0 commit comments