Giter VIP home page Giter VIP logo

hdb-pool's Introduction

Connection Pooling is fully supported by offical hana client, please use Node.js Connection Pooling instead

SAP HANA Database Connection Pool for Node

npm Travis Coverage Status npm downloads MIT licensed

HANA Database Connection pool for Node.js, inspired by (and copied some ideas from): Generic Pool.

This module supports hana-client and node-hdb. If both exist, the hana-client will be chosen.

Table of contents

Install

npm install hdb-pool

Getting started

This is an example how to use this module:

// import the module
const Pool = require('hdb-pool');

// HANA connection info
const dbParams = {
    hostName: 'hana-server-name',
    port: '30015',
    userName: 'user-name',
    password: 'user-password'
};

// pool options
const options = {
    min: 2,
    max: 15,
};

// create the pool
const pool = Pool.createPool(dbParams, options);

// execute some sample sql via the pool 
pool.getConnection()
    .then(conn => {
        conn.exec('select current_timestamp from dummy', (err, rows) => {
          //return the connection back to pool  
          pool.release(client);
            if (err) {
                // error handling
            } else {
                // handle the result: rows
            }
        });
    })
    .catch(err => {
        // error handling
    });

Creating a pool

The pool constructor takes two arguments:

  • dbParams: a dictionary containing the HANA DB connection information.
  • options : a dictionary containing the configuration for the Pool
const Pool = require('hdb-pool');
const pool = Pool.createPool(dbParams, options);

dbParams

A dictionary with following properties:

  • hostName: host name of HANA server.
  • port: port number.
  • userName: user name.
  • password: password.

options

An optional dictionary with the any of the following properties:

  • max: maximum number of resources to create at any given time. (default=50)
  • min: minimum number of resources to keep in pool at any given time. (default=3)
  • maxWaitingRequests: maximum number of waiting requests allowed. (default=0, no limit)
  • requestTimeout: max milliseconds a request will wait for a resource before timing out. (default=5000)
  • checkInterval: how often to run resource timeout checks. (default=0, disabled)
  • idleTimeout: the time of a connection staying idle in the pool that eligible for eviction. (default=30000)
  • debug: a flag for emitting those debug message. (default=false, disabled)

Getting a connection

pool.getConnection()
    .then(conn => {...})
    .catch(err => {...});

Getting a HANA connection from the pool, the getConnecction does not have any argument.

It returns a Promise. The promise will be resolved with a connection if the connection is available in the pool. And the promise will be rejected with an error if the pool is unable to give a connection(eg: timeout).

Returning a connection

pool.release(connection)
    .then(() => {...})
    .catch(err => {...});

Returning a connection to the pool, the release takes one required argument:

  • connection: a 'borrowed' connection.

This function returns a Promise. This promise will resolve once the connection is accepted by the pool, or reject if the pool is unable to accept the connection for any reason (e.g connection is not a resource that came from the pool). If you do not care the outcome it is safe to ignore this promise.

Destroying a connection

pool.destroy(connection)
    .then(() => {...})
    .catch(err => {...});

Removing the connection from the pool and destroy the connection itself as well. The function takes one required argument:

  • connection: a "borrowed" connection.

This function returns a Promise. This promise will resolve once the connection is accepted by the pool, If you do not care the outcome it is safe to ignore this promise.

Clearing the pool

pool.clear()
    .then(() => {...})
    .catch(err => {...});

This function clears the pool, removing/destroying all the connections and all the pending requests from the pool.

Receiving events from pool

Pool.eventEmitter.on('poolDebug', myEventHandler);
Pool.eventEmitter.on('poolError', myEventHandlerError);
Pool.eventEmitter.on('connectionCreateError', myEventHandlerCreateError);
Pool.eventEmitter.on('connectionValidationError', myEventHandlerValidateError);
Pool.eventEmitter.on('requestTimeout', myEventHandlerValidateError);

Pool supports 5 different types of events:

  • poolDebug: debug information of the pool, needs to be enabled by options.debug first.
  • poolError: error information of the pool.
  • connectionCreateError: connection creation error.
  • connectionValidationError: connection validation error.
  • requestTimeout: request timeout.

Getting status overview of pool

const overview = pool.getPoolStatusOverview();

This function will show current status of the pool.

Running tests

npm install
npm test

License

MIT

hdb-pool's People

Contributors

ckyycc avatar szbuli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hdb-pool's Issues

Request timeout issue occurring sporadically

During execution of method getConnection() on pool object, we receive the below error

"Request timeout. Request info: (id: 54, creation time: 1599638811640)","type":"log","custom_fields":{"stack":"Error: Request timeout. Request info: (id: 54, creation time: 1599638811640)\n at Request._fireTimeout (/home/vcap/app/node_modules/hdb-pool/lib/Request.js:114:17)\n at Timeout. (/home/vcap/app/node_modules/hdb-pool/lib/Request.js:92:43)\n at listOnTimeout (internal/timers.js:549:17)\n at processTimers (internal/timers.js:492:7)"}}

As this is sporadically occurring, I am not quite sure what causes this issue. The only way to resolve this as of now is to re-start the application.

Pool status object
"pool":"{"size":0,"min":2,"max":10,"available":0,"timeout":0}","request":"{"number":1,"pending":0,"max":0,"resolved":53,"rejected":0,"timeout":2}"}

Could you please help me understand how can we resolve this? This is causing downtime for our application.

Thanks!

hdb-pool does not disclose dependencies correctly

The full explanation for this issue can be found here.

Basically, when using yarn with pnp, yarn is really strict with dependency management, so you should not require dependencies which are not correctly declared in the package.json. As of now, hdb-pool requires @sap/hana-client in Utils but it does not declare it as a dependency in the package.json, only as a devDependency.

Because of this, a yarn pnp project trying to use any package that depends on hdb-pool (like typeorm) will fail and it needs to do a workaround in .yarnrc.yml:

...
packageExtensions:
  "hdb-pool@^0.1.6":
    dependencies:
      "@sap/hana-client": "^2.9.28"

To fix this, please just move hdb-pool from a devDependency to a dependency. For now, I'm not sure if any other package should also make this move.

Pool recovery

We recently had what seems like a connection hiccup to our HANA database (connection reset). While the database was reachable shortly after, it seems like all connections in the pool failed and did not recover, leading to a downtime.

Can you comment on the intended behavior? What can we do to make sure the pool automatically recovers from such failures and connections are correctly invalidated and recreated? I was unable to find information on this topic.

We are using hdb-pool in the context of typeorm.

Thanks!

Clearing Pool doesn't remove event listeners

Event listeners are not removed when the connection is closed and pool.clear() is triggered.

I'm using typeorm SapDriver, and when the connection is opened and then closed over 10 times (in tests) than I'm getting this warning:

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 poolError listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit

Also it seems that PoolManager.eventEmitter() shouldn't be a static method, because in case of multi-tenants scenario, errors will be shown for every tenant connection.

Whitesource scans failing because @sap/hana-client is a peer dependency

Hi Team,
Firstly I would like to thank you for developing this module! It is very much helpful because it supports both hdb and @sap/hana-client module.
I am using the hdb module for database connections but the whitesource scans are failing because hdb-pool has a peer dependency on @sap/hana-client module.
Due to a known bug in hana-client module I am forced to use hdb module.

Could you please help me tackle this?

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.