MCPcopy
hub / github.com/sequelize/sequelize / verifySelectLockInShareMode

Function verifySelectLockInShareMode

test/integration/transaction.test.js:1040–1150  ·  test/integration/transaction.test.js::verifySelectLockInShareMode
()

Source from the content-addressed store, hash-verified

1038
1039 it(class="st">'supports for share (i.e. `SELECT ... LOCK IN SHARE MODE`)', async function() {
1040 const verifySelectLockInShareMode = async () => {
1041 const User = this.sequelize.define(class="st">'user', {
1042 username: DataTypes.STRING,
1043 awesome: DataTypes.BOOLEAN
1044 }, { timestamps: false });
1045
1046 await this.sequelize.sync({ force: true });
1047 const { id } = await User.create({ username: class="st">'jan' });
1048
1049 class="cm">// First, we start a transaction T1 and perform a SELECT with it using the `LOCK.SHARE` mode (setting a shared mode lock on the row).
1050 class="cm">// This will cause other sessions to be able to read the row but not modify it.
1051 class="cm">// So, if another transaction tries to update those same rows, it will wait until T1 commits (or rolls back).
1052 class="cm">// https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
1053 const t1 = await this.sequelize.transaction();
1054 await User.findByPk(id, { lock: t1.LOCK.SHARE, transaction: t1 });
1055
1056 class="cm">// Then we start another transaction T2 and see that it can indeed read the same row.
1057 const t2 = await this.sequelize.transaction({ isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED });
1058 const t2Jan = await User.findByPk(id, { transaction: t2 });
1059
1060 class="cm">// Then, we want to see that an attempt to update that row from T2 will be queued until T1 commits.
1061 const executionOrder = [];
1062 const [t2AttemptData, t1AttemptData] = await pSettle([
1063 (async () => {
1064 try {
1065 executionOrder.push(class="st">'Begin attempt to update via T2');
1066 await t2Jan.update({ awesome: false }, { transaction: t2 });
1067 executionOrder.push(class="st">'Done updating via T2');
1068 } catch (error) {
1069 executionOrder.push(class="st">'Failed to update via T2'); class="cm">// Shouldn't happen
1070 throw error;
1071 }
1072
1073 await delay(30);
1074
1075 try {
1076 executionOrder.push(class="st">'Attempting to commit T2');
1077 await t2.commit();
1078 executionOrder.push(class="st">'Done committing T2');
1079 } catch {
1080 executionOrder.push(class="st">'Failed to commit T2'); class="cm">// Shouldn't happen
1081 }
1082 })(),
1083 (async () => {
1084 await delay(100);
1085
1086 try {
1087 executionOrder.push(class="st">'Begin attempt to read via T1');
1088 await User.findAll({ transaction: t1 });
1089 executionOrder.push(class="st">'Done reading via T1');
1090 } catch (error) {
1091 executionOrder.push(class="st">'Failed to read via T1'); class="cm">// Shouldn't happen
1092 throw error;
1093 }
1094
1095 await delay(150);
1096
1097 try {

Callers 1

Calls 8

defineMethod · 0.80
transactionMethod · 0.80
findByPkMethod · 0.80
commitMethod · 0.80
findAllMethod · 0.80
syncMethod · 0.45
createMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected