Skip to content

Commit def6e90

Browse files
committed
Added support for connection.ping()
1 parent 15d699a commit def6e90

File tree

4 files changed

+116
-6
lines changed

4 files changed

+116
-6
lines changed

doc/api.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ limitations under the License.
131131
- 4.2.6.4.3 [`resultSet`](#execresultset)
132132
- 4.2.6.4.4 [`rows`](#execrows)
133133
- 4.2.6.4.5 [`rowsAffected`](#execrowsaffected)
134-
- 4.2.7 [`queryStream()`](#querystream)
135-
- 4.2.8 [`release()`](#release)
136-
- 4.2.9 [`rollback()`](#rollback)
134+
- 4.2.7 [`ping()`](#connectionping)
135+
- 4.2.8 [`queryStream()`](#querystream)
136+
- 4.2.9 [`release()`](#release)
137+
- 4.2.10 [`rollback()`](#rollback)
137138
5. [Lob Class](#lobclass)
138139
- 5.1 [Lob Properties](#lobproperties)
139140
- 5.1.1 [`chunkSize`](#proplobchunksize)
@@ -2405,7 +2406,51 @@ the number of rows affected, for example the number of rows
24052406
inserted. For non-DML statements such as queries, or if no rows are
24062407
affected, then `rowsAffected` will appear as undefined.
24072408

2408-
#### <a name="querystream"></a> 4.2.7 `connection.queryStream()`
2409+
#### <a name="connectionping"></a> 4.2.7 `connection.ping()`
2410+
2411+
##### Prototype
2412+
2413+
Callback:
2414+
```
2415+
ping(function(Error error){});
2416+
```
2417+
Promise:
2418+
```
2419+
promise = ping();
2420+
```
2421+
2422+
##### Description
2423+
2424+
This method checks that a connection is currently usable and the
2425+
network to the database is valid. This call can be useful for system
2426+
health checks. A ping only confirms that a single connection is
2427+
usable at the time of the ping.
2428+
2429+
Pinging doesn't replace error checking during statement execution,
2430+
since network or database failure may occur in the time between
2431+
`ping()` and `execute()` calls.
2432+
2433+
Pinging requires a 'round trip' to the database so unnecessary ping
2434+
calls should be avoided.
2435+
2436+
If `ping()` returns an error, the application should close the
2437+
connection.
2438+
2439+
This method was added in node-oracledb 2.2.
2440+
2441+
##### Parameters
2442+
2443+
```
2444+
function(Error error)
2445+
```
2446+
2447+
The parameters of the callback function are:
2448+
2449+
Callback function parameter | Description
2450+
----------------------------|-------------
2451+
*Error error* | If `ping()` succeeds, `error` is NULL. If an error occurs, then `error` contains the [error message](#errorobj).
2452+
2453+
#### <a name="querystream"></a> 4.2.8 `connection.queryStream()`
24092454

24102455
##### Prototype
24112456

@@ -2444,11 +2489,11 @@ This method was added in node-oracledb 1.8.
24442489

24452490
See [execute()](#execute).
24462491

2447-
#### <a name="release"></a> 4.2.8 `connection.release()`
2492+
#### <a name="release"></a> 4.2.9 `connection.release()`
24482493

24492494
An alias for [connection.close()](#connectionclose).
24502495

2451-
#### <a name="rollback"></a> 4.2.9 `connection.rollback()`
2496+
#### <a name="rollback"></a> 4.2.10 `connection.rollback()`
24522497

24532498
##### Prototype
24542499

@@ -3591,6 +3636,9 @@ allows a valid connection to be opened by some subsequent
35913636
You can tune `poolPingInterval` to meet your quality of service
35923637
requirements.
35933638
3639+
Explicit pings on any connection can be performed at any time with
3640+
[`connection.ping()`](#connectionping).
3641+
35943642
### <a name="drcp"></a> 8.4 Database Resident Connection Pooling (DRCP)
35953643
35963644
[Database Resident Connection Pooling][24] (DRCP) enables database
@@ -3897,6 +3945,7 @@ application being aware of any service disruption.
38973945
For a more information on FAN see the [whitepaper on Fast Application
38983946
Notification][97].
38993947
3948+
39003949
#### <a name="connectionrlb"></a> 8.9.2 Runtime Load Balancing (RLB)
39013950
39023951
[Oracle Database RAC][93] users with [Oracle Database (RLB)][65]

lib/connection.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var rollbackPromisified;
2929
var closePromisified;
3030
var breakPromisified;
3131
var changePasswordPromisified;
32+
var pingPromisified;
3233

3334
// The queryStream function is similar to execute except that it immediately
3435
// returns a QueryStream.
@@ -265,6 +266,18 @@ function changePassword(user, password, newPassword, changePasswordCb) {
265266

266267
changePasswordPromisified = nodbUtil.promisify(changePassword);
267268

269+
// This ping function is just a place holder to allow for easier extension later.
270+
function ping(pingCb) {
271+
var self = this;
272+
273+
nodbUtil.assert(arguments.length === 1, 'NJS-009');
274+
nodbUtil.assert(typeof pingCb === 'function', 'NJS-006', 1);
275+
276+
self._ping.apply(self, arguments);
277+
}
278+
279+
pingPromisified = nodbUtil.promisify(ping);
280+
268281
// The extend method is used to extend the Connection instance from the C layer with
269282
// custom properties and method overrides. References to the original methods are
270283
// maintained so they can be invoked by the overriding method at the right time.
@@ -347,6 +360,14 @@ function extend(conn, oracledb, pool) {
347360
value: changePasswordPromisified,
348361
enumerable: true,
349362
writable: true
363+
},
364+
_ping: {
365+
value: conn.ping
366+
},
367+
ping: {
368+
value: pingPromisified,
369+
enumerable: true,
370+
writable: true
350371
}
351372
}
352373
);

src/njsConnection.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void njsConnection::Init(Handle<Object> target)
8686
Nan::SetPrototypeMethod(tpl, "break", Break);
8787
Nan::SetPrototypeMethod(tpl, "createLob", CreateLob);
8888
Nan::SetPrototypeMethod(tpl, "changePassword", ChangePassword);
89+
Nan::SetPrototypeMethod(tpl, "ping", Ping);
8990

9091
Nan::SetAccessor(tpl->InstanceTemplate(),
9192
Nan::New<v8::String>("stmtCacheSize").ToLocalChecked(),
@@ -1764,6 +1765,41 @@ void njsConnection::Async_ChangePassword(njsBaton *baton)
17641765
}
17651766

17661767

1768+
//-----------------------------------------------------------------------------
1769+
// njsConnection::Ping()
1770+
// Ping the database to see if it is "alive".
1771+
//
1772+
// PARAMETERS
1773+
// - JS callback which will receive (error)
1774+
//-----------------------------------------------------------------------------
1775+
NAN_METHOD(njsConnection::Ping)
1776+
{
1777+
njsConnection *connection;
1778+
njsBaton *baton;
1779+
1780+
connection = (njsConnection*) ValidateArgs(info, 1, 1);
1781+
if (!connection)
1782+
return;
1783+
baton = connection->CreateBaton(info);
1784+
if (!baton)
1785+
return;
1786+
if (baton->error.empty())
1787+
baton->SetDPIConnHandle(connection->dpiConnHandle);
1788+
baton->QueueWork("Ping", Async_Ping, NULL, 1);
1789+
}
1790+
1791+
1792+
//-----------------------------------------------------------------------------
1793+
// njsConnection::Async_Ping()
1794+
// Worker function for njsConnection::Ping() method.
1795+
//-----------------------------------------------------------------------------
1796+
void njsConnection::Async_Ping(njsBaton *baton)
1797+
{
1798+
if (dpiConn_ping(baton->dpiConnHandle) < 0)
1799+
baton->GetDPIError();
1800+
}
1801+
1802+
17671803
//-----------------------------------------------------------------------------
17681804
// njsConnection::GetStmtCacheSize()
17691805
// Get accessor of "stmtCacheSize" property.

src/njsConnection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class njsConnection: public njsCommon {
124124
static NAN_METHOD(ChangePassword);
125125
static void Async_ChangePassword(njsBaton *baton);
126126

127+
// Ping Method on Connection class
128+
static NAN_METHOD(Ping);
129+
static void Async_Ping(njsBaton *baton);
130+
127131
// Define Getter Accessors to properties
128132
static NAN_GETTER(GetStmtCacheSize);
129133
static NAN_GETTER(GetClientId);

0 commit comments

Comments
 (0)