Code Monkey home page Code Monkey logo

tedious-connection-pool's Introduction

tedious-connection-pool

A connection pool for tedious.

Installation

npm install mhingston/tedious-connection-pool

Description

The API is almost identical to the official tedious-connection-pool. Rather than creating a connection and then closing it when finished, you acquire a connection from the pool and release it when finished.

Once the Tedious Connection object has been acquired, the tedious API can be used with the connection as normal.

Example (callback)

var ConnectionPool = require('tedious-connection-pool');
var Request = require('tedious').Request;

var poolConfig = {
    min: 2,
    max: 4,
    log: true
};

var connectionConfig = {
    userName: 'login',
    password: 'password',
    server: 'localhost'
};

//create the pool
var pool = new ConnectionPool(poolConfig, connectionConfig);

//acquire a connection
pool.acquire(function (error, connection) {
    if (error) {
        console.error(error);
        return;
    }

    //use the connection as normal
    var request = new Request('select 42', function(error, rowCount) {
        if (error) {
            console.error(error);
            return;
        }

        console.log('rowCount: ' + rowCount);

        //release the connection back to the pool when finished
        connection.release();
    });

    request.on('row', function(columns) {
        console.log('value: ' + columns[0].value);
    });

    connection.execSql(request);
});

Example (promise)

var ConnectionPool = require('tedious-connection-pool');
var Request = require('tedious').Request;

var poolConfig = {
    min: 2,
    max: 4,
    log: true
};

var connectionConfig = {
    userName: 'login',
    password: 'password',
    server: 'localhost'
};

//create the pool
var pool = new ConnectionPool(poolConfig, connectionConfig);

//acquire a connection
pool.acquire()
.then(function (connection) {
    //use the connection as normal
    var request = new Request('select 42', function(error, rowCount) {
        if (error) {
            console.error(error);
            return;
        }

        console.log('rowCount: ' + rowCount);

        //release the connection back to the pool when finished
        connection.release();
    });

    request.on('row', function(columns) {
        console.log('value: ' + columns[0].value);
    });

    connection.execSql(request);
})
.catch(function (error) {
    console.error(error);
});

When you are finished with the pool, you can drain it (close all connections).

pool.drain();

Class: ConnectionPool

new ConnectionPool(poolConfig, connectionConfig)

  • poolConfig {Object} the pool configuration object

    • min {Number} The minimun of connections there can be in the pool. Default = 10
    • max {Number} The maximum number of connections there can be in the pool. Default = 50
    • idleTimeout {Number} The number of milliseconds before closing an unused connection. Default = 30000
    • acquireTimeout {Number} The number of milliseconds to wait for a connection, before returning an error. Default = 60000
    • log {Boolean|Function} Set to true to have debug log written to the console or pass a function to receive the log messages. Default = false
  • connectionConfig {Object} The same configuration that would be used to create a tedious Connection.

connectionPool.acquire(callback)

Acquire a Tedious Connection object from the pool. This method returns a promise, but you can use a callback function if you prefer.

  • callback(error, connection) {Function} Callback function
  • error {Object} Object containing an error that occurred whilst trying to acquire a connection, otherwise null.
  • connection {Object} A Connection

connectionPool.drain()

Close all pooled connections and stop making new ones. The pool should be discarded after it has been drained.

Class: Connection

The following method is added to the Tedious Connection object.

Connection.release()

Release the connection back into the pool to be used again.

tedious-connection-pool's People

Contributors

mhingston avatar

Watchers

James Cloos avatar

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.