{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/4105","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/4105/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/4105/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/4105/events","html_url":"https://github.com/sequelize/sequelize/issues/4105","id":94773852,"number":4105,"title":"Transactions, Tedious and Connection Pools","user":{"login":"awolden","id":1420506,"avatar_url":"https://avatars.githubusercontent.com/u/1420506?v=3","gravatar_id":"","url":"https://api.github.com/users/awolden","html_url":"https://github.com/awolden","followers_url":"https://api.github.com/users/awolden/followers","following_url":"https://api.github.com/users/awolden/following{/other_user}","gists_url":"https://api.github.com/users/awolden/gists{/gist_id}","starred_url":"https://api.github.com/users/awolden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awolden/subscriptions","organizations_url":"https://api.github.com/users/awolden/orgs","repos_url":"https://api.github.com/users/awolden/repos","events_url":"https://api.github.com/users/awolden/events{/privacy}","received_events_url":"https://api.github.com/users/awolden/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/sequelize/sequelize/labels/bug","name":"bug","color":"d7e102"},{"url":"https://api.github.com/repos/sequelize/sequelize/labels/dependency","name":"dependency","color":"c7def8"},{"url":"https://api.github.com/repos/sequelize/sequelize/labels/mssql","name":"mssql","color":"bfdadc"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2015-07-13T17:46:47Z","updated_at":"2016-01-12T18:29:14Z","closed_at":null,"body":"I have run into an issue making concurrent model calls while in a transaction when using the MSSQL dialect (tedious).\r\n\r\nThe Error I get: \r\n\r\n    Unhandled rejection SequelizeDatabaseError: Requests can only be made in the LoggedIn state, not the SentClientRequest state\r\n        at Query.formatError (/home/woldena/Desktop/testapp/node_modules/sequelize/lib/dialects/mssql/query.js:215:10)\r\n        at Request.userCallback (/home/woldena/Desktop/testapp/node_modules/sequelize/lib/dialects/mssql/query.js:46:25)\r\n        at Request.callback (/home/woldena/Desktop/testapp/node_modules/tedious/lib/request.js:30:27)\r\n        at Connection.makeRequest (/home/woldena/Desktop/testapp/node_modules/tedious/lib/connection.js:1142:22)\r\n        at Connection.commitTransaction (/home/woldena/Desktop/testapp/node_modules/tedious/lib/connection.js:1048:17)\r\n        at /home/woldena/Desktop/testapp/node_modules/sequelize/lib/dialects/mssql/query.js:44:25\r\n        at tryCatcher (/home/woldena/Desktop/testapp/node_modules/sequelize/node_modules/bluebird/js/main/util.js:26:23)\r\n        at Promise._resolveFromResolver (/home/woldena/Desktop/testapp/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:462:31)\r\n        at new Promise (/home/woldena/Desktop/testapp/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:69:37)\r\n        at Query.run (/home/woldena/Desktop/testapp/node_modules/sequelize/lib/dialects/mssql/query.js:33:17)\r\n        at /home/woldena/Desktop/testapp/node_modules/sequelize/lib/sequelize.js:713:18\r\n        at tryCatcher (/home/woldena/Desktop/testapp/node_modules/sequelize/node_modules/bluebird/js/main/util.js:26:23)\r\n        at Promise._settlePromiseFromHandler (/home/woldena/Desktop/testapp/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:489:31)\r\n        at Promise._settlePromiseAt (/home/woldena/Desktop/testapp/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:565:18)\r\n        at Promise._settlePromiseAtPostResolution (/home/woldena/Desktop/testapp/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:244:10)\r\n        at Async._drainQueue (/home/woldena/Desktop/testapp/node_modules/sequelize/node_modules/bluebird/js/main/async.js:128:12)\r\n\r\nAfter some research I found out this error occurs when tedious tries to make simultaneous requests on the same connection, so it seems like the transaction isn't utilizing the connection pool.\r\n\r\nHow I can produce it:\r\n\r\n```javascript\r\n'use strict';\r\n\r\nconst app = require('../../app'),\r\n    Sequelize = require('sequelize'),\r\n    config = require(`${__base}/config`),\r\n    sequelize = new Sequelize(config.db.dbName, config.db.user, config.db.password, config.sequelize),\r\n    _ = require('lodash'),\r\n\r\n    //models\r\n    models = require(`${__base}/models`);\r\n\r\n\r\n    /*-----\r\n    This works\r\n    -------*/\r\n    const promiseQueue = [];\r\n\r\n    promiseQueue.push(models.Organization.findAll());\r\n    promiseQueue.push(models.Individual.findAll());\r\n\r\n    Promise.all(promiseQueue).then(function(results){\r\n      console.log(results);\r\n    }, function(err){\r\n      console.log(err);\r\n    });\r\n\r\n\r\n    /*-----\r\n    This Does Not\r\n    -------*/\r\n    sequelize.transaction(function(t){\r\n      const promiseQueue = [];\r\n\r\n      promiseQueue.push(models.Organization.findAll({transaction:t}));\r\n      promiseQueue.push(models.Individual.findAll({transaction:t}));\r\n\r\n      return Promise.all(promiseQueue).then(function(results){\r\n        console.log(results);\r\n      }, function(err){\r\n        console.log(err);\r\n      });\r\n    });\r\n```\r\n\r\nI'm not sure if this is a bug or if transactions do not implicitly support connection pooling.\r\n\r\nP.S. Here are my config settings:\r\n\r\n```javascript\r\n  sequelize: {\r\n        host: 'localhost',\r\n        dialect: 'mssql',\r\n        dialectOptions: {\r\n            requestTimeout: 60000\r\n        },\r\n        pool:{\r\n          maxConnections:15,\r\n          minConnections:5,\r\n          maxIdleTime:10000\r\n        }\r\n    }\r\n```"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/171003478","html_url":"https://github.com/sequelize/sequelize/issues/4105#issuecomment-171003478","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/4105","id":171003478,"user":{"login":"treythomas123","id":2589390,"avatar_url":"https://avatars.githubusercontent.com/u/2589390?v=3","gravatar_id":"","url":"https://api.github.com/users/treythomas123","html_url":"https://github.com/treythomas123","followers_url":"https://api.github.com/users/treythomas123/followers","following_url":"https://api.github.com/users/treythomas123/following{/other_user}","gists_url":"https://api.github.com/users/treythomas123/gists{/gist_id}","starred_url":"https://api.github.com/users/treythomas123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/treythomas123/subscriptions","organizations_url":"https://api.github.com/users/treythomas123/orgs","repos_url":"https://api.github.com/users/treythomas123/repos","events_url":"https://api.github.com/users/treythomas123/events{/privacy}","received_events_url":"https://api.github.com/users/treythomas123/received_events","type":"User","site_admin":false},"created_at":"2016-01-12T18:29:14Z","updated_at":"2016-01-12T18:29:14Z","body":"+1\r\n\r\nJust hit this. When I used `Promise.all` with transaction queries, I was expecting the queries to be queued, because clearly there's no way to actually run them in parallel within a transaction. After reading through https://github.com/pekim/tedious/issues/318, it still seems to me like Tedious should be handling this, just like the `mysql` and `pg` libraries.\r\n\r\nI'll take a look and possibly write a PR for handling this within Sequelize."}}