Giter Site home page Giter Site logo

Comments (4)

ben-page avatar ben-page commented on June 11, 2024

Hello, will you please add a complete example? Your example code doesn't do what your output shows. Also, please be sure to include your pool config.

from tedious-connection-pool.

ben-page avatar ben-page commented on June 11, 2024

Just FYI, the connection-pool will not give you the same connection again until you release it. I'm not sure why you think that it's the same connection, but that's not how the connection pool works.

One thing that I noticed when trying to run your example, is that you are setting i to 0 on every call to execute_SELECT_Statement. So i is always 0 when accessed.

from tedious-connection-pool.

boredSinan avatar boredSinan commented on June 11, 2024

Thanks a lot ben-page,
now my code works fine with your tip (i is always set to 0) as follows:

Changes:

" i " is called now " loop_count " and it is set to 0 at the end of any execute_SELECT_Statement()-call.

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


/*
 *  Config varibale which includes important information about the Microsoft SQL database.
 *  Without port number specification, there wont be a connection!
 *
 */
var connectionConfig = {
    server: 'MSserver',
    userName: 'user',
    password: 'password',
    options: {
        database: 'databaseName',
        port: 55555
    }
};

var poolConfig = {
    min: 1,
    max: 10,
    log: true
};

//Connection objects of the Microsoft SQL database.
//You need several connections, because 1 single connection is not able to work concurrent!
//Define a pool:
var pool = new ConnectionPool(poolConfig, connectionConfig);

/*
 *  db_uid_start: fixed first bytes for the MS-SQL datatype 'UniqueIdentifier'
 *  db_uid_end:   last bytes of the UID
 *
 */
var db_uid_start = '00000000-0000-0000-0000-';
var db_uid_end = '';

var sql_select_result_original = '';
var sql_select_result_fake = '';

var select_result = [];
var loop_count = 0;


//Make a Ms-SQL uniqueidentifier string format
//e.g. 00000000-0000-0000-0000-207754F45231
db_uid_end = db_uid_start.concat(peripheral.uuid);

//Execute SQL SELECT command
execute_SELECT_Statement('SELECT UID FROM table');
execute_SELECT_Statement('SELECT Name FROM table');
execute_SELECT_Statement('SELECT Address FROM table');
execute_SELECT_Statement('SELECT Brand FROM table');
execute_SELECT_Statement('SELECT Location FROM table');

display_Select_Result(select_result);

/*
 * Function which selects from all database-tables
 * Do not execute: "SELECT *       FROM table"
 * Always execute: "SELECT column  FROM table"
 */
function execute_SELECT_Statement(sql_statement) {

    pool.acquire(function(err, connection) {
        if (err)
            console.error(err);

        //use the connection as normal
        var request = new Request(sql_statement, function(err, rowCount) {
            if (err)
                console.error(err);
            //console.log('rowCount: ' + rowCount);

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

        connection.execSql(request);

        request.on('row', function(columns) {
            //console.log('value: ' + columns[0].value);
            console.log('loop_count: ' + loop_count);
            select_result[loop_count] = columns[0].value;
            loop_count++;
        });
        //pool.drain();
    });
   loop_count = 0;
}

/*
 * Function which displays content of any array
 */
function display_Select_Result(select_result) {
    for (var l = 0; l < select_result.length; l++) {
        console.log('select_result: ' + select_result[l] + '\n');
    }
    select_result = null;
    select_result = [];
}

from tedious-connection-pool.

ben-page avatar ben-page commented on June 11, 2024

This code has a lot of problems.

  • There is no guarantee the queries will return in the order you called them. They are just appended to the end of select_result, but that could be any order. For example, the query for Brand could easily return before the query for Address.
  • If there are multiple rows in table this would become even more problematic. The values from the columns in all the rows would just stuffed in select_result in a entirely unpredictable order.
  • display_Select_Result will never display anything. pool.acquire is asynchronous. It's callback will always be executed after the call to display_Select_Result is called in your code.
  • You don't need to do multiple queries to the same table. You can select multiple columns in a single query.
SELECT UID, Name FROM table

from tedious-connection-pool.

Related Issues (20)

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.