Я пытаюсь вот так
try {
let t;
t = await db.sequelize.transaction();
let event1 = await db.EventModel.findOne(
{
where: { id: 1 }
},
{
transaction: t
}
);
let event2 = await db.EventModel.findOne({ where: { id: 1 } });
await event2.update({ event_name: "test2" });
setTimeout(async () => {
await event1.update({ event_name: "test1" }, { transaction: t });
await t.commit();
console.log("??????");
}, 10000);
} catch (error) {
console.log("??????", error);
}
это данные журнала
result before update event2 {
id: 1,
event_name: 'make event name',
createdAt: 2020-01-23T08:25:12.000Z,
updatedAt: 2020-01-26T18:02:33.000Z
}
result after update event2 {
id: 1,
event_name: 'test2',
createdAt: 2020-01-23T08:25:12.000Z,
updatedAt: 2020-01-26T18:03:59.000Z
}
result after update event1 {
id: 1,
event_name: 'test1'
createdAt: 2020-01-23T08:25:12.000Z,
updatedAt: 2020-01-26T18:04:09.000Z
}
??????
Строка event.id = 1 не заблокирована.
Поэтому я пытаюсь
try {
let t;
t = await db.sequelize.transaction();
let event1 = await db.EventModel.findOne({
where: { id: 1 },
transaction: t,
lock: {
level: t.LOCK.NO_KEY_UPDATE
}
});
let event2 = await db.EventModel.findOne({ where: { id: 1 } });
await event2.update({ event_name: "test2" });
setTimeout(async () => {
await event1.update({ event_name: "test1" }, { transaction: t });
await t.commit();
console.log("??????");
}, 10000);
} catch (error) {
console.log("??????", error);
}
подождите 50 секунд, затем журнал моего приложения будет выглядеть следующим образом
?????? DatabaseError [SequelizeDatabaseError]: Lock wait timeout exceeded; try restarting transaction
at Query.formatError (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/dialects/mysql/query.js:244:16)
at Execute.handler [as onResult] (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/dialects/mysql/query.js:51:23)
at Execute.execute (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/commands/command.js:30:14)
at Connection.handlePacket (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:417:32)
at PacketParser.onPacket (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:75:12)
at PacketParser.executeStart (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:82:25)
at Socket.emit (events.js:219:5)
at addChunk (_stream_readable.js:332:12)
at readableAddChunk (_stream_readable.js:307:11)
at Socket.Readable.push (_stream_readable.js:241:10)
at TCP.onStreamRead (internal/stream_base_commons.js:185:23)
From previous event:
at Query.run (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/dialects/mysql/query.js:39:12)
at /Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/sequelize.js:645:29
From previous event:
at /Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/sequelize.js:645:12
From previous event:
at /Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/sequelize.js:641:10
at /Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/retry-as-promised/index.js:70:21
at new Promise (<anonymous>)
at retryAsPromised (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/retry-as-promised/index.js:60:10)
at /Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/sequelize.js:631:30
From previous event:
at Sequelize.query (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/sequelize.js:580:23)
at QueryInterface.update (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/query-interface.js:1012:27)
at /Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/model.js:3985:52
at processImmediate (internal/timers.js:444:21)
From previous event:
at EventModel.save (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/model.js:3962:8)
at EventModel.update (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/sequelize/lib/model.js:4176:17)
at module.exports (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/src/databaseLoader/index.js:19:18)
at async /Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/server.js:78:5 {
name: 'SequelizeDatabaseError',
parent: Error: Lock wait timeout exceeded; try restarting transaction
at Packet.asError (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/packets/packet.js:712:17)
at Execute.execute (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/commands/command.js:28:26)
at Connection.handlePacket (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:417:32)
at PacketParser.onPacket (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:75:12)
at PacketParser.executeStart (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:82:25)
at Socket.emit (events.js:219:5)
at addChunk (_stream_readable.js:332:12)
at readableAddChunk (_stream_readable.js:307:11)
at Socket.Readable.push (_stream_readable.js:241:10)
at TCP.onStreamRead (internal/stream_base_commons.js:185:23) {
code: 'ER_LOCK_WAIT_TIMEOUT',
errno: 1205,
sqlState: 'HY000',
sqlMessage: 'Lock wait timeout exceeded; try restarting transaction',
sql: 'UPDATE `events` SET `event_name`=?,`updatedAt`=? WHERE `id` = ?',
parameters: [ 'test2', '2020-01-26 17:50:36', 1 ]
},
original: Error: Lock wait timeout exceeded; try restarting transaction
at Packet.asError (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/packets/packet.js:712:17)
at Execute.execute (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/commands/command.js:28:26)
at Connection.handlePacket (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:417:32)
at PacketParser.onPacket (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:75:12)
at PacketParser.executeStart (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/Users/akkarapongkhamtanet/Documents/MyWorks/joindi/back-end/node_modules/mysql2/lib/connection.js:82:25)
at Socket.emit (events.js:219:5)
at addChunk (_stream_readable.js:332:12)
at readableAddChunk (_stream_readable.js:307:11)
at Socket.Readable.push (_stream_readable.js:241:10)
at TCP.onStreamRead (internal/stream_base_commons.js:185:23) {
code: 'ER_LOCK_WAIT_TIMEOUT',
errno: 1205,
sqlState: 'HY000',
sqlMessage: 'Lock wait timeout exceeded; try restarting transaction',
sql: 'UPDATE `events` SET `event_name`=?,`updatedAt`=? WHERE `id` = ?',
parameters: [ 'test2', '2020-01-26 17:50:36', 1 ]
},
sql: 'UPDATE `events` SET `event_name`=?,`updatedAt`=? WHERE `id` = ?',
parameters: [ 'test2', '2020-01-26 17:50:36', 1 ]
}
Я хочу заблокировать строку при открытии транзакции .
Я пытался прочитать документ (https://sequelize.org/master/class/lib/transaction.js~Transaction.html#static -get-LOCK ) и строка блокировки не могут использовать блокировку.
помогите мне, пожалуйста, я прочитал документ