Skip to content

Commit 183396c

Browse files
committed
Added a method connection.isHealthy()
1 parent cda2b24 commit 183396c

File tree

7 files changed

+205
-76
lines changed

7 files changed

+205
-76
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
**This release is under development and information may be incomplete**
66

7+
8+
- Added a [`connection.isHealthy()`](https://oracle.github.io/node-oracledb/doc/api.html#ishealthy)
9+
function to perform a local connection health check
10+
711
- Fixed numeric suffix feature (for duplicate SELECT column names when using
812
`oracledb.OUT_FORMAT_OBJECT` mode) when the column name is also a JavaScript
913
property or method name.

doc/api.md

Lines changed: 104 additions & 74 deletions
Large diffs are not rendered by default.

lib/connection.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved
1+
// Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved
22

33
//-----------------------------------------------------------------------------
44
//
@@ -453,7 +453,6 @@ async function tpcRollback(xid) {
453453
}
454454

455455

456-
457456
// define class
458457
class Connection extends EventEmitter {
459458

@@ -541,6 +540,18 @@ class Connection extends EventEmitter {
541540
return (this._getSodaDatabase());
542541
}
543542

543+
//--------------------------------------------------------------------------
544+
// isHealthy()
545+
// To get the health status of the connection.
546+
// NOTE: if this function returns false, the application must close the
547+
// connection obect.
548+
// This is a synchronous call.
549+
//---------------------------------------------------------------------------
550+
isHealthy() {
551+
return this._isHealthy();
552+
}
553+
554+
544555
// The queryStream function is similar to execute except that it immediately
545556
// returns a QueryStream.
546557
queryStream(sql, binding, options) {

src/njsConnection.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static NJS_NAPI_METHOD(njsConnection_getDbObjectClass);
3737
static NJS_NAPI_METHOD(njsConnection_getQueue);
3838
static NJS_NAPI_METHOD(njsConnection_getSodaDatabase);
3939
static NJS_NAPI_METHOD(njsConnection_getStatementInfo);
40+
static NJS_NAPI_METHOD(njsConnection_isHealthy);
4041
static NJS_NAPI_METHOD(njsConnection_ping);
4142
static NJS_NAPI_METHOD(njsConnection_rollback);
4243
static NJS_NAPI_METHOD(njsConnection_shutdown);
@@ -156,6 +157,8 @@ static const napi_property_descriptor njsClassProperties[] = {
156157
NULL, napi_default, NULL },
157158
{ "_getStatementInfo", NULL, njsConnection_getStatementInfo, NULL,
158159
NULL, NULL, napi_default, NULL },
160+
{ "_isHealthy", NULL, njsConnection_isHealthy, NULL, NULL, NULL,
161+
napi_default, NULL },
159162
{ "_ping", NULL, njsConnection_ping, NULL, NULL, NULL, napi_default,
160163
NULL },
161164
{ "_rollback", NULL, njsConnection_rollback, NULL, NULL, NULL,
@@ -2108,6 +2111,28 @@ bool njsConnection_newFromBaton(njsBaton *baton, napi_env env,
21082111
}
21092112

21102113

2114+
//-----------------------------------------------------------------------------
2115+
// njsConnection_isHealthy()
2116+
// Get the Health status - whether the current connection is usable or not
2117+
// NOTE: If this function returns FALSE, conn.close() has to be called
2118+
//-----------------------------------------------------------------------------
2119+
static napi_value njsConnection_isHealthy(napi_env env,
2120+
napi_callback_info info)
2121+
{
2122+
int value = 0;
2123+
napi_value connObj;
2124+
njsConnection *conn;
2125+
2126+
if (njsUtils_validateArgs(env, info, 0, NULL, &connObj,
2127+
(njsBaseInstance **)&conn)) {
2128+
if (conn->handle) {
2129+
dpiConn_getIsHealthy(conn->handle, &value);
2130+
}
2131+
}
2132+
return njsUtils_convertToBoolean(env, value);
2133+
}
2134+
2135+
21112136
//-----------------------------------------------------------------------------
21122137
// njsConnection_ping()
21132138
// Ping the database to see if it is "alive".

test/connHealthy.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Copyright (c) 2022, 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+
* The node-oracledb test suite uses 'mocha'.
19+
* See LICENSE.md for relevant licenses.
20+
*
21+
* NAME
22+
* 261. connHealthy.js
23+
*
24+
* DESCRIPTION
25+
* Test cases for connecton.isHealthy()
26+
*
27+
*****************************************************************************/
28+
'use strict';
29+
30+
const oracledb = require('oracledb');
31+
const assert = require('assert');
32+
const dbconfig = require('./dbconfig.js');
33+
34+
describe('261. connHealthy.js', function() {
35+
36+
describe('261.1 connection health on stand alone connections', function() {
37+
it('261.1.1 connection health on good connection', async function() {
38+
const conn = await oracledb.getConnection(dbconfig);
39+
const isHealthy = conn.isHealthy();
40+
assert.strictEqual(isHealthy, true);
41+
await conn.close();
42+
});
43+
44+
it('261.1.2 connection health on closed connection', async function() {
45+
const conn = await oracledb.getConnection(dbconfig);
46+
await conn.close();
47+
const isHealthy = conn.isHealthy();
48+
assert.strictEqual(isHealthy, false);
49+
});
50+
51+
});
52+
53+
});

test/list.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5291,3 +5291,8 @@ oracledb.OUT_FORMAT_OBJECT and resultSet = true
52915291

52925292
260. tpcResume.js
52935293
260.1 TPC suspend and resume
5294+
5295+
261. connHealthy.js
5296+
261.1 connection health on stand alone connections
5297+
261.1.1 connection health on good connection
5298+
261.1.2 connection health on closed connection

test/opts/.mocharc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,4 @@ spec:
247247
- test/keepInStmtCache.js
248248
- test/tpc.js
249249
- test/tpcResume.js
250+
- test/connHealthy.js

0 commit comments

Comments
 (0)