{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/4342","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/4342/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/4342/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/4342/events","html_url":"https://github.com/sequelize/sequelize/issues/4342","id":101461268,"number":4342,"title":"Transaction has been started twice","user":{"login":"bossqone","id":71897,"avatar_url":"https://avatars.githubusercontent.com/u/71897?v=3","gravatar_id":"","url":"https://api.github.com/users/bossqone","html_url":"https://github.com/bossqone","followers_url":"https://api.github.com/users/bossqone/followers","following_url":"https://api.github.com/users/bossqone/following{/other_user}","gists_url":"https://api.github.com/users/bossqone/gists{/gist_id}","starred_url":"https://api.github.com/users/bossqone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bossqone/subscriptions","organizations_url":"https://api.github.com/users/bossqone/orgs","repos_url":"https://api.github.com/users/bossqone/repos","events_url":"https://api.github.com/users/bossqone/events{/privacy}","received_events_url":"https://api.github.com/users/bossqone/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-08-17T16:37:52Z","updated_at":"2015-08-17T19:44:38Z","closed_at":null,"body":"Hello, I'm storing tweets from twitter with their authors and sometimes transaction run twice as you can see below. Problem is only with saving \"owner's info\" (table `FeedOwners`) - handled in `.getOwnerInfo()`, duplicity in `Feed` table is okay (different `eid`).\r\n\r\nThanks for every response.\r\n\r\n```sql\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): START TRANSACTION;\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): START TRANSACTION;\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): SET autocommit = 1;\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): SET autocommit = 1;\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): SELECT `id`, `accountId`, `facebookId`, `facebookToken`, `twitterId`, `twitterToken   `, `twitterSecret`, `googleId`, `googleToken`, `eventbriteId`, `eventbriteToken`, `instagramId`, `instagramToken` FROM `AccountTokens   ` AS `AccountTokens` WHERE `AccountTokens`.`twitterId` = '24596994' LIMIT 1;\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): SELECT `id`, `accountId`, `facebookId`, `facebookToken`, `twitterId`, `twitterToken   `, `twitterSecret`, `googleId`, `googleToken`, `eventbriteId`, `eventbriteToken`, `instagramId`, `instagramToken` FROM `AccountTokens   ` AS `AccountTokens` WHERE `AccountTokens`.`twitterId` = '24596994' LIMIT 1;\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): SAVEPOINT `630caa2b-0b52-4bdd-80dd-27d9c707a256-savepoint-1`;\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): SAVEPOINT `e928bd92-4407-4623-b851-2003d1222719-savepoint-1`;\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): SELECT `id`, `type`, `originId`, `name`, `picture` FROM `FeedOwners` AS `FeedOwners   ` WHERE `FeedOwners`.`type` = 0 AND `FeedOwners`.`originId` = '24596994' LIMIT 1;\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): SELECT `id`, `type`, `originId`, `name`, `picture` FROM `FeedOwners` AS `FeedOwners   ` WHERE `FeedOwners`.`type` = 0 AND `FeedOwners`.`originId` = '24596994' LIMIT 1;\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): INSERT INTO `FeedOwners` (`id`,`type`,`originId`,`name`,`picture`) VALUES (DEFAULT,   0,'24596994','JolitaBrilliant','http://pbs.twimg.com/profile_images/619310070745182209/RksZCOKb.jpg');\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): INSERT INTO `FeedOwners` (`id`,`type`,`originId`,`name`,`picture`) VALUES (DEFAULT,   0,'24596994','JolitaBrilliant','http://pbs.twimg.com/profile_images/619310070745182209/RksZCOKb.jpg');\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): INSERT INTO `Feed` (`id`,`originId`,`aid`,`oid`,`eid`,`type`,`text`,`created`,`link   `) VALUES (DEFAULT,'633301305830809600',NULL,1142,1,0,'#test #driving #jaguar #2015 #red #thats a #test #car! #auto #sweet #smooth #s   ports #engine #damn it… https://t.co/ZlcNDYnlF1','2015-08-17 15:36:12','https://twitter.com/statuses/633301305830809600');\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): INSERT INTO `Feed` (`id`,`originId`,`aid`,`oid`,`eid`,`type`,`text`,`created`,`link   `) VALUES (DEFAULT,'633301305830809600',NULL,1142,2,0,'#test #driving #jaguar #2015 #red #thats a #test #car! #auto #sweet #smooth #s   ports #engine #damn it… https://t.co/ZlcNDYnlF1','2015-08-17 15:36:12','https://twitter.com/statuses/633301305830809600');\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): SELECT `Feed`.*, `FeedContents`.`id` AS `FeedContents.id`, `FeedContents`.`fid` AS    `FeedContents.fid`, `FeedContents`.`type` AS `FeedContents.type`, `FeedContents`.`content` AS `FeedContents.content`, `FeedLikes`.`id   ` AS `FeedLikes.id`, `FeedLikes`.`fid` AS `FeedLikes.fid`, `FeedLikes`.`aid` AS `FeedLikes.aid`, `FeedLikes`.`created` AS `FeedLikes.   created`, `FeedComments`.`id` AS `FeedComments.id`, `FeedComments`.`fid` AS `FeedComments.fid`, `FeedComments`.`aid` AS `FeedComments   .aid`, `FeedComments`.`text` AS `FeedComments.text`, `FeedComments`.`created` AS `FeedComments.created`, `FeedOwner`.`id` AS `FeedOwn   er.id`, `FeedOwner`.`type` AS `FeedOwner.type`, `FeedOwner`.`originId` AS `FeedOwner.originId`, `FeedOwner`.`name` AS `FeedOwner.name   `, `FeedOwner`.`picture` AS `FeedOwner.picture` FROM (SELECT `Feed`.`id`, `Feed`.`originId`, `Feed`.`aid`, `Feed`.`oid`, `Feed`.`eid`   , `Feed`.`type`, `Feed`.`text`, `Feed`.`created`, `Feed`.`link` FROM `Feed` AS `Feed` WHERE `Feed`.`eid` = 1 AND `Feed`.`type` = 0 AN   D `Feed`.`originId` = '633301305830809600' LIMIT 1) AS `Feed` LEFT OUTER JOIN `FeedContent` AS `FeedContents` ON `Feed`.`id` = `FeedC   ontents`.`fid` LEFT OUTER JOIN `FeedLikes` AS `FeedLikes` ON `Feed`.`id` = `FeedLikes`.`fid` LEFT OUTER JOIN `FeedComments` AS `FeedC   omments` ON `Feed`.`id` = `FeedComments`.`fid` LEFT OUTER JOIN `FeedOwners` AS `FeedOwner` ON `Feed`.`oid` = `FeedOwner`.`id`;\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): SELECT `Feed`.*, `FeedContents`.`id` AS `FeedContents.id`, `FeedContents`.`fid` AS    `FeedContents.fid`, `FeedContents`.`type` AS `FeedContents.type`, `FeedContents`.`content` AS `FeedContents.content`, `FeedLikes`.`id   ` AS `FeedLikes.id`, `FeedLikes`.`fid` AS `FeedLikes.fid`, `FeedLikes`.`aid` AS `FeedLikes.aid`, `FeedLikes`.`created` AS `FeedLikes.   created`, `FeedComments`.`id` AS `FeedComments.id`, `FeedComments`.`fid` AS `FeedComments.fid`, `FeedComments`.`aid` AS `FeedComments   .aid`, `FeedComments`.`text` AS `FeedComments.text`, `FeedComments`.`created` AS `FeedComments.created`, `FeedOwner`.`id` AS `FeedOwn   er.id`, `FeedOwner`.`type` AS `FeedOwner.type`, `FeedOwner`.`originId` AS `FeedOwner.originId`, `FeedOwner`.`name` AS `FeedOwner.name   `, `FeedOwner`.`picture` AS `FeedOwner.picture` FROM (SELECT `Feed`.`id`, `Feed`.`originId`, `Feed`.`aid`, `Feed`.`oid`, `Feed`.`eid`   , `Feed`.`type`, `Feed`.`text`, `Feed`.`created`, `Feed`.`link` FROM `Feed` AS `Feed` WHERE `Feed`.`eid` = 2 AND `Feed`.`type` = 0 AN   D `Feed`.`originId` = '633301305830809600' LIMIT 1) AS `Feed` LEFT OUTER JOIN `FeedContent` AS `FeedContents` ON `Feed`.`id` = `FeedC   ontents`.`fid` LEFT OUTER JOIN `FeedLikes` AS `FeedLikes` ON `Feed`.`id` = `FeedLikes`.`fid` LEFT OUTER JOIN `FeedComments` AS `FeedC   omments` ON `Feed`.`id` = `FeedComments`.`fid` LEFT OUTER JOIN `FeedOwners` AS `FeedOwner` ON `Feed`.`oid` = `FeedOwner`.`id`;\r\nExecuting (630caa2b-0b52-4bdd-80dd-27d9c707a256): COMMIT;\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): SELECT `id`, `type`, `originId`, `name`, `picture` FROM `FeedOwners` AS `FeedOwners   ` WHERE `FeedOwners`.`type` = 0 AND `FeedOwners`.`originId` = '24596994' LIMIT 1;\r\nExecuting (e928bd92-4407-4623-b851-2003d1222719): ROLLBACK;\r\nUnhandled rejection SequelizeUniqueConstraintError: Validation error\r\n    at Query.formatError (/opt/pribe/node_modules/sequelize/lib/dialects/mysql/query.js:134:14)\r\n    at Query._callback (/opt/pribe/node_modules/sequelize/lib/dialects/mysql/query.js:35:21)\r\n    at Query.Sequence.end (/opt/pribe/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)\r\n    at Query.ErrorPacket (/opt/pribe/node_modules/mysql/lib/protocol/sequences/Query.js:94:8)\r\n    at Protocol._parsePacket (/opt/pribe/node_modules/mysql/lib/protocol/Protocol.js:271:23)\r\n    at Parser.write (/opt/pribe/node_modules/mysql/lib/protocol/Parser.js:77:12)\r\n    at Protocol.write (/opt/pribe/node_modules/mysql/lib/protocol/Protocol.js:39:16)\r\n    at Socket.<anonymous> (/opt/pribe/node_modules/mysql/lib/Connection.js:96:28)\r\n    at Socket.emit (events.js:107:17)\r\n    at readableAddChunk (_stream_readable.js:163:16)\r\n    at Socket.Readable.push (_stream_readable.js:126:10)\r\n    at TCP.onread (net.js:538:20)\r\n```\r\n\r\nCode block with .transaction:\r\n```js\r\nreturn this.db.Sequelize.transaction(function (transaction) {\r\n    return _this.getOwnerInfo(tweet.user, transaction).then(function (ownerInfo) {\r\n        return Promise.all(_this.subscribers[tag].map(function (eventId) {\r\n            return _this.handleTweetPerEvent(eventId, tag, tweet, ownerInfo, transaction);\r\n        }));\r\n    });\r\n});\r\n```\r\n\r\nCode block with .getOwnerInfo():\r\n```js\r\nTwitter.prototype.getOwnerInfo = function (owner, transaction) {\r\n    var _this = this;\r\n\r\n    return this.db.AccountTokens.findOne({\r\n        where: {\r\n            twitterId: owner.id_str\r\n        },\r\n        raw: true,\r\n        transaction: transaction\r\n    }).then(function (tokens) {\r\n        if (!tokens) {\r\n            return _this.db.FeedOwners.findOrCreate({\r\n                where: {\r\n                    type: _this.config.feedEnum.twitter,\r\n                    originId: owner.id_str\r\n                },\r\n                defaults: {\r\n                    name: owner.name,\r\n                    picture: owner.profile_image_url.replace('_normal', '')\r\n                },\r\n                transaction: transaction\r\n            }).spread(function (ownerObj, created) {\r\n                return {\r\n                    accountId: null,\r\n                    ownerId: ownerObj.id\r\n                }\r\n            });\r\n        }\r\n\r\n        return {\r\n            accountId: tokens.accountId,\r\n            ownerId: null\r\n        };\r\n    });\r\n};\r\n```"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/131940178","html_url":"https://github.com/sequelize/sequelize/issues/4342#issuecomment-131940178","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/4342","id":131940178,"user":{"login":"bossqone","id":71897,"avatar_url":"https://avatars.githubusercontent.com/u/71897?v=3","gravatar_id":"","url":"https://api.github.com/users/bossqone","html_url":"https://github.com/bossqone","followers_url":"https://api.github.com/users/bossqone/followers","following_url":"https://api.github.com/users/bossqone/following{/other_user}","gists_url":"https://api.github.com/users/bossqone/gists{/gist_id}","starred_url":"https://api.github.com/users/bossqone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bossqone/subscriptions","organizations_url":"https://api.github.com/users/bossqone/orgs","repos_url":"https://api.github.com/users/bossqone/repos","events_url":"https://api.github.com/users/bossqone/events{/privacy}","received_events_url":"https://api.github.com/users/bossqone/received_events","type":"User","site_admin":false},"created_at":"2015-08-17T19:44:37Z","updated_at":"2015-08-17T19:44:37Z","body":"I'm using exactly same code as above. Most of time it works great, but sometimes it just fails."}}