1

I am trying to create a connection pool using the node module hana-client/hdbext.

Is there any way to set the max and min pool size using these modules.

Can somebody share a example or a document i can follow for this

I am able to connect to Hana using hana-client with config pooling set to true. But not sure how can i set the max and min pool size and idle time

modules used

"@sap/hana-client": "^2.4.182" "@sap/hdbext": "^6.0.1"

Thanks

Arun

Suncatcher
  • 10,355
  • 10
  • 52
  • 90

2 Answers2

1

The 3 parameters are pooling, maxPoolSize and connectionLifetime according to the driver documentation for nodejs driver with hana client 2.4.x

Remi sap
  • 144
  • 6
0

I have tried setting up connection pooling using @sap/hana-client library - both approaches implicit, explicit and neither work. So I ended up with this implementation:

import { Connection, createConnection, ConnectionOptions } from '@ibsolution/types-hana-client';
import { createPool, Pool, Factory, Options } from 'generic-pool';

export class HanaService {
    private readonly pool: Pool<Connection>;

    constructor(config: CONFIG) {
        this.pool = HanaService.createConnectionPool(HanaService.getConnectionOptions(config));
    }

    private static createConnectionPool(connectionOptions: ConnectionOptions): Pool<Connection> {
        const factory: Factory<Connection> = {
            create(): Promise<Connection> {
                const connection = createConnection();

                return new Promise((resolve, reject) =>
                    connection.connect(connectionOptions, (err: Error) => {
                        if (err) {
                            reject(err);
                        }
                        resolve(connection);
                    }),
                );
            },
            destroy(connection: Connection): Promise<void> {
                return new Promise((resolve, reject) =>
                    connection.disconnect((err: Error) => {
                        if (err) {
                            reject(err);
                        }
                        resolve();
                    }),
                );
            },
        };

        const opts: Options = {
            max: 50,
            min: 5,
            idleTimeoutMillis: 60000,
            autostart: true,
        };

        return createPool(factory, opts);
    }

    private static getConnectionOptions(config: CONFIG): ConnectionOptions {
        return {
            pwd: 'PWD',
            uid: 'UID',
            host: 'HOST',
            port: 'PORT',
        };
    }

    async query<T extends object>(sql: string, sqlArgs?: unknown[]): Promise<T[]> {
        const conn = await this.pool.acquire();

        return new Promise((resolve, reject) => {
            conn.exec(sql, sqlArgs, (err, rows) => {
                this.pool.release(conn);

                if (err) {
                    reject(err);
                }
                resolve(rows as T[]);
            });
        });
    }
}
Ondrej11
  • 53
  • 1
  • 10