Repository navigation
Description
What are you doing?
// the following code shows what the problem is, even though it may not run
//with sequelize and a pool like:
pool:{
max: 10, //Never have more than x open connections
min: 3, //At a minimum, have x open connections/maintain no minimum number of connections
idle: 1000*3600 //Remove a connection from the pool after the connection has been idle (not been used) for x milli seconds
},
// repeat the following 2 line 3 (=minimum connection in pool) times with newName = "Foo", "Bar", then "Foo Bar"
module.exports.index = async function (req,res){
let user = await db.user.findOne({where:{id:1}});
try{
let transaction = await db.sequelize.transaction({autocommit:false});
await user.update({name:req.body.newName},{transaction:transaction});
await transaction.commit();
}
catch(e){
await transaction.rollback();
}
}
// finally execute this many times
module.exports.index2 = async function (req,res){
let user = await db.user.findOne({where:{id:1}});
res.send("the name of the user is : "+user.name);
}
What do you expect to happen?
I wanted :
"the name of the user is : Foo Bar"
"the name of the user is : Foo Bar"
"the name of the user is : Foo Bar"
"the name of the user is : Foo Bar"
"the name of the user is : Foo Bar"
"the name of the user is : Foo Bar"
"the name of the user is : Foo Bar"
What is actually happening?
But the output was :
"the name of the user is : Foo"
"the name of the user is : Bar"
"the name of the user is : Foo Bar"
"the name of the user is : Foo"
"the name of the user is : Bar"
"the name of the user is : Foo Bar"
"the name of the user is : Foo"
"the name of the user is : Bar"
The problem was solved by encapsulating all the findOne in a transaction like so:
module.exports.index = async function (req,res){
try{
let transaction = await db.sequelize.transaction({autocommit:false});
let user = await db.user.findOne({where:{id:1},transaction:transaction});
await user.update({name:req.body.newName},{transaction:transaction});
await transaction.commit();
}
catch(e){
await transaction.rollback();
}
}
module.exports.index2 = async function (req,res){
try{
let transaction = await db.sequelize.transaction({autocommit:false});
let user = await db.user.findOne({where:{id:1},transaction:transaction});
res.send("the name of the user is : "+user.name);
await transaction.commit();
}
catch(e){
await transaction.rollback();
}
}
findOne (and I guess other functions) are not autocommit:true (they have a hidden running transaction), this means that when a pool has more than one connection, and you run a findOne with one of them, they will have uncommited transactions that will soon induce the returning wrong values of the attributes you have changed in the meantime (only true for strict isolation levels). The output value will be dependent on which connection it was from. When new findOne will be used, it will be on a random connection of the pool, if some update where made on other connections they won't be taken into account if a previous findOne was made on the current connection.
example:
connexion A: findOne => "Foo"
connexion B: update => "Bar"
connextion A : findOne=>"Foo"
with transactions (what it sould be by default):
connexion A: findOne => "Foo"
connexion B: update => "Bar"
connextion A: findOne=>"Bar"
for people having similar issues, printing transaction.connection.connectionId was helpfull to understand the link between the connection pool, transaction, and the values change...
I am not looking for an answer except if someone has some constructive criticism on my code or my comprehension, I only hope that it may help someone one day. I have lost a lot of time figuring this out and wish to avoid it to someone else.
I hope that I successfully re transcribed the necessary code and comments so that the problem is understandable and reproducible.
Dialect: mysql
Dialect version: latest
Database version: latest
Sequelize version: 4.43.0
Some keywords to help people find it:
sequelize mysql postgresql ACID transaction isolation level wrong value model update save findone findall commit rollback database connection pool different disable cache previous DataValues values status aftercommit asynchronous promise await previousDataValues
Activity
sushantdhiman commentedon Mar 4, 2019
Hey @AmbroiseVuaridel , I think this was fixed by #9921 Can you please try
sequelize@next(will install v5-beta) and see if that works for you.AmbroiseVuaridel commentedon Mar 4, 2019
Hey @sushantdhiman , thank you, glad to hear you are working on it! For now my project depends on https://www.npmjs.com/package/sequelize-hierarchy which has a problem with version 5, when I have some time I will retry to make it reproducible in v5
AmbroiseVuaridel commentedon Mar 4, 2019
I have found also that the line where the transaction is created impact the result, if you call let transaction = await db.sequelize.transaction({autocommit:false}); before the findOne, it works as expected, not if you call it afterward, even if you don't pass the transaction in options of findOne...
qqaimh commentedon May 8, 2019
I find another bugger.
`
try{
let transaction = await dbseq.sequelize.transaction({autocommit:false});
The log
`Executing (9469eb2d-0453-42d6-b76a-f044d7db81f6): START TRANSACTION;
Executing (9469eb2d-0453-42d6-b76a-f044d7db81f6): SELECT
email,password,salt,tiehanhan,costmoney,nickname,phonenumber,createAt,updateAt,version,statusFROMUsersASUsersWHEREUsers.nickname= '123';Executing (9469eb2d-0453-42d6-b76a-f044d7db81f6): UPDATE
UsersSETcostmoney=? WHEREemail= ?Executing (9469eb2d-0453-42d6-b76a-f044d7db81f6): COMMIT;`
papb commentedon Aug 1, 2019
Closing this since I believe the documentation on transactions has improved.
papb commentedon Aug 1, 2019
@qqaimh If you are having issues, please open a new issue following our bug report template.
ccemeraldeyes commentedon Sep 6, 2019
@papb I disagree that documentation on transactions is sufficient now. I was still assuming that single operations would have their own transactions by default. I would strongly suggest adding disclaimers in multiple places saying that without some kind of manual intervention, sequelize uses ZERO TRANSACTIONS.
papb commentedon Sep 6, 2019
@ccemeraldeyes Ok. I will do this very soon. Thanks!
AmbroiseVuaridel commentedon Sep 8, 2019
@papb Thanks !