there is a table in the database. I read this table first. If there is anything that meets the conditions, I will directly return. If not, update a piece of data. Now, if two requests come in, neither of them will read the qualified data. The expected result is that the first request is not read, one will be updated, and the second request should read the result of the first insert. According to the netizen"s suggestion, I opened the transaction and set it to read dirty, but the result is still the same, the following code:
sequlize.transaction({ autocommit: true, isolationLevel: sequlize.Transaction.ISOLATION_LEVELS.READ_UNCOMMITTED }, async function (t) {
//
return sequlize.models.chat_user.findOne({ where: { url: data.url, have_vest: 1, vest_name: data.vest_name, online: 1, room: room }, raw: true, transaction: t })
.then(function (vest) {
if (vest) {
io.sockets.connected[socket.id].emit("rob_vest", { success: false, message: "not avilable vest" })
// return sequlize.Promise.reject();
return
}
return sequlize.models.used_vest.findOne({ where: { phone, room, url }, raw: true, transaction: t })
.then(async function (cool_vest) {
//
if (!cool_vest) {
console.log("*userd_time", vest_time)
let used_time = vest_time
console.log("userddddddddtime------------------", used_time)
let data = await sequlize.models.used_vest.create({ phone, room, url, used_time })
}
return sequlize.models.chat_user.update({ have_vest: 1, url: data.url, vest_name: data.vest_name, vest_time: vest_time, room: room }, { where: { phone }, raw: true, transaction: t })
.then(function (update_user) {
console.log("")
if (update_user) {
//
io.to(room).emit("rob_vest", { success: true, phone: phone })
schedules.addListenVest(io)
} else {
//
io.sockets.connected[socket.id].emit("rob_vest", { success: false, phone: phone })
}
})
})
});
}).then(function (results) {
/* */
console.log("")
}).catch(function (err) {
/* */
throw err
console.log("", err)
});;
},
The table of operation is the chat_ user table, and two requests come in at the same time. As a result, two pieces of data are updated
.