Skip to content

Parallel Requests using Multiple Connections #236

Closed

Description

@ivantodorovich

Hello,

I'm trying to execute multiple requests using multiple connections. I don't really know if I'm doing it wrong.

After calling the function query 2 times in parallel, I always get the results of the last one called in both callbacks. No matter the parameters I set.

If I invert the functions calls, I keep getting the result of the last one called.. so SQL is correct.

query ( require('./config.js'), 1, 0, function (err, data) {
    if (!!err) throw err;
    console.log(data);

})

query ( require('./config.js'), 1, 1, function (err, data) {
    if (!!err) throw err;
    console.log(data);
})

function query (config, param1, param2, callback) {

    config.query = ""
    config.query += 'DECLARE @Param1  INT = '+param1+';'
    config.query += 'DECLARE @Param2  INT = '+param2+';'
    config.query += fs.readFileSync('query.sql');

    // Connect to database
    var connection = new tedious.Connection({
        userName: config.username,
        password: config.password,
        server  : config.server,
        options : {
            instanceName: config.instance,
            databaseName: config.database,
        }

    // Work
    }).on('connect', function (err) {
        if (!!err) return callback(err);

        var data = []

        var request = new tedious.Request(config.query, function (err) {
            if (!!err) return callback(err);
            callback(null, data);
            connection.close();
        })

        request.on('row', function (columns) {
            var row = {};
            for (i in columns) {
                row[columns[i].metadata.colName] = columns[i].value
                if (columns[i].metadata.type.name == 'BigInt')
                    row[columns[i].metadata.colName] = parseInt(columns[i].value)
            }
            data.push(row)
        })

        connection.execSql(request)
    })
}

Somewhat related to: #35 although I'm trying to use the method suggested in the comments.

Activity

  1. ivantodorovich commented on Jan 28, 2015

    @ivantodorovich
    Author

    Ok, after a few testing I managed to solve it. Here's how I fixed it, in case anyone needs it.

    Don't create the new request inside the 'connect' event. Instead, do it next to the connection and then execute inside the 'connect' event.

    It does feel like a bug, though.. I don't really know why it didn't work before. Am I doing something wrong with scopes?

        // Connect to database
        var connection = new tedious.Connection({
            userName: config.username,
            password: config.password,
            server  : config.server,
            options : {
                instanceName: config.instance,
                databaseName: config.database,
            }
        })
    
        // Prepare Request
        var data = []
    
        // Handle Callback
        var request = new tedious.Request(config.query, function (err) {
            if (!!err) return callback(err);
            callback(null, data);
            connection.close();
        })
    
        // Handle Row
        request.on('row', function (columns) {
            var row = {};
            for (i in columns) {
                row[columns[i].metadata.colName] = columns[i].value
                if (columns[i].metadata.type.name == 'BigInt')
                    row[columns[i].metadata.colName] = parseInt(columns[i].value)
            }
            data.push(row)
        })
    
        // Execute
        connection.on('connect', function (err) {
            if (!!err) return callback(err);
            connection.execSql(request)
        })

    Closing after this.

  2. bretcope commented on Jan 28, 2015

    @bretcope
    Member

    The reason you're getting the same results in the first case is because you're running the same query. If you use a local variable for the SQL string, then that won't happen.

    Here's what you're experiencing:

    • query called. Assigns query1 to config.query and opens connection1.
    • query called. Assigns query2 to config.query and opens connection2.
    • connection1 connects and runs a request with the value of config.query which is query2.
    • connection2 connects and runs a request with the value of config.query which is query2.

    The reason it works when you create the request in the query function is because you capture the value of config.query before it is changed by the second call to query()

  3. ivantodorovich commented on Jan 28, 2015

    @ivantodorovich
    Author

    Ohhh, I'm such an idiot!
    It's amazingly clear now that you've explained it.

    Thank you, @bretcope

  4. naiieandrade commented on Nov 19, 2018

    @naiieandrade

    Hey @ivantodorovich or anyelse, if you could help me.

    I'm using tedious with sql-server, and I have to return a json file.
    The #1 problem I had was because my select was returning 3 rows because was a relation one-to-many tables.

    For example:

    id id_model_car
    1 3
    1 2
    2 1
    2 2
    2 4
    3 4

    The select was some like

    SELECT * FROM table where id=1
    

    Was returning 3 rows, but it's was justing showing in Postman the last row because was being replaced.

    And I need this return:

    {
      "id": 200,
      "name": "Jennifer Whalen",
      "isSenior": true,
      "commissionPct": null,
      "jobHistory": [
        {
          "id": "AD_ASST",
          "departmentId": 90,
          "startDate": "17-SEP-1995",
          "endDate": "17-JUN-2001"
        },
        {
          "id": "AC_ACCOUNT",
          "departmentId": 90,
          "startDate": "01-JUL-2002",
          "endDate": "31-DEC-2006"
        }
      ]
    } 
    

    To do this, I think I need two selects. One to get id, name, isSenior, commissionPct and another to put in a list for jobHistory.

    Do you can help me?

  5. saketnimbargi commented on Mar 2, 2020

    @saketnimbargi

    @naiieandrade could you share your code snippet for better understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions