()
| 1092 | |
| 1093 | it('supports for share (i.e. `SELECT ... LOCK IN SHARE MODE`)', async function() { |
| 1094 | const verifySelectLockInShareMode = async () => { |
| 1095 | const User = this.sequelize.define('user', { |
| 1096 | username: DataTypes.STRING, |
| 1097 | awesome: DataTypes.BOOLEAN |
| 1098 | }, { timestamps: false }); |
| 1099 | |
| 1100 | await this.sequelize.sync({ force: true }); |
| 1101 | const { id } = await User.create({ username: 'jan' }); |
| 1102 | |
| 1103 | // 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). |
| 1104 | // This will cause other sessions to be able to read the row but not modify it. |
| 1105 | // So, if another transaction tries to update those same rows, it will wait until T1 commits (or rolls back). |
| 1106 | // https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html |
| 1107 | const t1 = await this.sequelize.transaction(); |
| 1108 | await User.findByPk(id, { lock: t1.LOCK.SHARE, transaction: t1 }); |
| 1109 | |
| 1110 | // Then we start another transaction T2 and see that it can indeed read the same row. |
| 1111 | const t2 = await this.sequelize.transaction({ isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED }); |
| 1112 | const t2Jan = await User.findByPk(id, { transaction: t2 }); |
| 1113 | |
| 1114 | // Then, we want to see that an attempt to update that row from T2 will be queued until T1 commits. |
| 1115 | const executionOrder = []; |
| 1116 | const [t2AttemptData, t1AttemptData] = await pSettle([ |
| 1117 | (async () => { |
| 1118 | try { |
| 1119 | executionOrder.push('Begin attempt to update via T2'); |
| 1120 | await t2Jan.update({ awesome: false }, { transaction: t2 }); |
| 1121 | executionOrder.push('Done updating via T2'); |
| 1122 | } catch (error) { |
| 1123 | executionOrder.push('Failed to update via T2'); // Shouldn't happen |
| 1124 | throw error; |
| 1125 | } |
| 1126 | |
| 1127 | await delay(30); |
| 1128 | |
| 1129 | try { |
| 1130 | executionOrder.push('Attempting to commit T2'); |
| 1131 | await t2.commit(); |
| 1132 | executionOrder.push('Done committing T2'); |
| 1133 | } catch { |
| 1134 | executionOrder.push('Failed to commit T2'); // Shouldn't happen |
| 1135 | } |
| 1136 | })(), |
| 1137 | (async () => { |
| 1138 | await delay(100); |
| 1139 | |
| 1140 | try { |
| 1141 | executionOrder.push('Begin attempt to read via T1'); |
| 1142 | await User.findAll({ transaction: t1 }); |
| 1143 | executionOrder.push('Done reading via T1'); |
| 1144 | } catch (error) { |
| 1145 | executionOrder.push('Failed to read via T1'); // Shouldn't happen |
| 1146 | throw error; |
| 1147 | } |
| 1148 | |
| 1149 | await delay(150); |
| 1150 | |
| 1151 | try { |
no test coverage detected